Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for Trying to use multiprocessing queue but unable to get the desired results

$
0
0

The while loop you have in your code is wrong because you don't want the function_to_get_from_q() process to quit everytime it checks the queue and it's empty. In the code below, a special value is put() into the queue to indicate that it's the last one.

import multiprocessing as mpimport datetime as dtSENTINEL = 'stop'def function_to_get_from_q(queue):    while (value := queue.get()) != SENTINEL:        print(value)def collect(queue):    for i in range(10000):        t = dt.datetime.utcnow() + dt.timedelta(hours=5, minutes=30)        queue.put([i, t.strftime('%H:%M:%S')])    queue.put(SENTINEL)  # Indicate end.if __name__ == "__main__":    queue = mp.Queue()    process1 = mp.Process(target=collect, args=(queue,))    process2 = mp.Process(target=function_to_get_from_q, args=(queue,))    process1.start()    process2.start()    print('fini')

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>