Is JoinableQueue.join() "process safe"?
Question:
If I have two processes communicating through a JoinableQueue, and I do the following:
process 1:
queue.put(1) #unfished tasks = 1
queue.join() #block until unfished tasks = 0
print('hello')
process 2:
queue.get()
queue.task_done() #unfished tasks = 0
queue.put(1) #unfinished tasks 1
the unfished tasks refers to what is written in the documentation.
will ‘hello’ always be printed? Or is there a chance that the put in process 2 executes before process 1 noticed that it should unblock?
It seems that the whole point of join() is that ‘hello’ should always be printed, but I just want to make sure that I understand it correctly.
Answers:
Queues should always have pure producers and pure consumers, not both. I suggest, that you use two queues. One for where P1 is producers and P2 the consumer and another vice versa.
Even not answering your question precisely, that would solve the problem i guess?
If you want the concrete answer:
will 'hello' always be printed?
– No
Or is there a chance that the put in process 2 executes before process 1 noticed that it should unblock?
– Yes
There is no guaranty, that P1 is continued (and the join
checks the size==0) between the task_done
and put
. It may works, it may not, it may is random, it may difference on machines or planetary constellations – that’s called nondeterminism
If I have two processes communicating through a JoinableQueue, and I do the following:
process 1:
queue.put(1) #unfished tasks = 1
queue.join() #block until unfished tasks = 0
print('hello')
process 2:
queue.get()
queue.task_done() #unfished tasks = 0
queue.put(1) #unfinished tasks 1
the unfished tasks refers to what is written in the documentation.
will ‘hello’ always be printed? Or is there a chance that the put in process 2 executes before process 1 noticed that it should unblock?
It seems that the whole point of join() is that ‘hello’ should always be printed, but I just want to make sure that I understand it correctly.
Queues should always have pure producers and pure consumers, not both. I suggest, that you use two queues. One for where P1 is producers and P2 the consumer and another vice versa.
Even not answering your question precisely, that would solve the problem i guess?
If you want the concrete answer:
will 'hello' always be printed?
– No
Or is there a chance that the put in process 2 executes before process 1 noticed that it should unblock?
– Yes
There is no guaranty, that P1 is continued (and the join
checks the size==0) between the task_done
and put
. It may works, it may not, it may is random, it may difference on machines or planetary constellations – that’s called nondeterminism