Multiprocessing Process os.system(‘kill -9 {0}’.format(pid)) fails on Linux

when I use terminate in linux with python, I found the way to close the progress in the way os.system(‘kill -9 {0}’.format(pid)), but it failed.

My code is to run two progress.when the first progress finished, I want the second progress close too (the second progress it’s self running all the time if you don’t close it).

sys.setrecursionlimit(1000000)
progress1 = Process(target=main)
progress2 = Process(target=run_proxypool)
progress1.daemon = True
progress2.start()
pid = progress2.pid
time.sleep(10)
progress1.start()
progress1.join()
os.system('kill -9 {0}'.format(pid))

Answer

Make sure you’re running on Linux. Also consider using os.kill(pid, signal.SIGKILL).

Not exactly what you’re asking, but consider switching to psutil for cross-platform goodness as well as conveniences such as wait_procs which will gracefully send SIGTERM, wait, then send SIGKILL.

signal.SIGKILL should be available on Linux (not on Windows).

The multiprocessing module recently gained a kill() method that falls back to terminate on Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *