user5740843
user5740843

Reputation: 1620

Python Mulitprocessing

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?

Upvotes: 0

Views: 64

Answers (2)

Hannes Ovr&#233;n
Hannes Ovr&#233;n

Reputation: 21851

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.

Upvotes: 1

RedBaron
RedBaron

Reputation: 4755

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()

Upvotes: 2

Related Questions