December 2018
Beginner to intermediate
796 pages
19h 54m
English
Let's now see how to communicate between processes using a queue. This example is very very similar to the one for threads:
# comm_queue_proc.pyimport multiprocessingSENTINEL = 'STOP'def producer(q, n): a, b = 0, 1 while a <= n: q.put(a) a, b = b, a + b q.put(SENTINEL)def consumer(q): while True: num = q.get() if num == SENTINEL: break print(f'Got number {num}')q = multiprocessing.Queue()cns = multiprocessing.Process(target=consumer, args=(q, ))prd = multiprocessing.Process(target=producer, args=(q, 35))cns.start()prd.start()
As you can see, in this case, we have to use a queue that is an instance of multiprocessing.Queue, which doesn't expose a task_done method. However, because of the way this queue ...
Read now
Unlock full access