1

I do:

import multiprocessing as mp
q = mp.Queue()
p = mp.Process(target=lambda x: x, args=('foo',))
p.start()
p.join(10)
if p.exitcode == 0:
   q.get()
else:
   print("We timed out.")

What I expect is that my tiny x->x function will block for at most 10 seconds. If it finished before then, I get the result sooner than that. If it takes longer than that (obviously not in this toy example, but in other cases), then the process terminates after 10 seconds and I am able to check exitcode to determine that we have indeed timed out.

But when I execute this code, what happens instead is that q.get() runs forever. Why does this occur?

1 Answer 1

1

q.get() runs forever since it blocks by default, and you never put anything into the queue.

You can use q.get(block=False) or q.get_nowait() (which is equivalent to the former). But remember that it will raise queue.Empty, so you should handle that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.