I'm currently experimenting with multiprocessing in Python, and I've made this small script:
import multiprocessing, time
def test_def():
time.sleep(5)
p = multiprocessing.Process(target=test_def())
print p, p.is_alive()
Yet, when I run it, it waits 5 secondes before it prints out:
<Process(Process-1, initial)> False
To me, this makes little sense. If p.start() isn't called, then how can the function be running?
2 Answers 2
When you do
p = multiprocessing.Process(target=test_def())
you actually call the function. The correct way, per the docs, would be
p = multiprocessing.Process(target=test_def)
p.start()
p.join()
Comments
The target= keyword expects a function to be called in the new process.
You are effectively passing it None since you have written target=test_def() and not target=test_def. The former gives the return value of your test_def() function after calling it (which is None).
You are also never actually starting your process, so you need to add a p.start() and p.join() as well.