Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic multiprocessing with infinity loop and queue

import random
import queue as Queue
import _thread as Thread

a = Queue.Queue()

def af():
    while True:
        a.put(random.randint(0,1000))

def bf():
    while True:
        if (not a.empty()): print (a.get())

def main():
    Thread.start_new_thread(af, ())
    Thread.start_new_thread(bf, ())
    return

if __name__ == "__main__":
    main()

the above code works fine with extreme high CPU usage, i tried to use multiprocessing with no avail. i have tried

def main():
    multiprocessing.Process(target=af).run()
    multiprocessing.Process(target=bf).run()

and

def main():
    manager = multiprocessing.Manager()
    a = manager.Queue()
    pool = multiprocessing.Pool()
    pool.apply_async(af)
    pool.apply_async(bf)

both not working, can anyone please help me? thanks a bunch ^_^

like image 877
tomato Avatar asked Sep 21 '26 12:09

tomato


1 Answers

def main():
    multiprocessing.Process(target=af).run()  # will not return
    multiprocessing.Process(target=bf).run()

The above code does not work because af does not return; no chance to call bf. You need to separate run call to start/join so that both can run in parallel. (+ to make them share manage.Queue)


To make the second code work, you need to pass a (manager.Queue object) to functions. Otherwise they will use Queue.Queue global object which is not shared between processes; need to modify af, bf to accepts a, and main to pass a.

def af(a):
    while True:
        a.put(random.randint(0, 1000))

def bf(a):
    while True:
        print(a.get())
def main():
    manager = multiprocessing.Manager()
    a = manager.Queue()
    pool = multiprocessing.Pool()
    proc1 = pool.apply_async(af, [a])
    proc2 = pool.apply_async(bf, [a])

    # Wait until process ends. Uncomment following line if there's no waiting code.
    # proc1.get()
    # proc2.get()
like image 128
falsetru Avatar answered Sep 24 '26 02:09

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!