0

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?

asked Apr 15, 2016 at 7:33

2 Answers 2

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()
answered Apr 15, 2016 at 7:36
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Apr 15, 2016 at 7:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.