[Python] Python Multiprocessing Pool Queue Works in Functional Code but not in OOP

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am learning multiprocessing in Python and am trying to incorporate a worker pool for managing downloads. I have deduced my queue issue down to something with OOP, but I don't know what it is. The following is my unit test to verify functionality. The functional code works as expected even with simulated, delayed jobs. However, the same translation into OOP does not exhibit the same results.

    Question:


    Why does the OOP incorporation return nothing out of the message queue for the process pool?

    Problem:


    Methods to incorporate multiprocessing pool queues in OOP do no behave the same as functional incorporations.

    Working Functional Code:


    import multiprocessing

    def worker(name, que):
    que.put("%d is done" % name)

    if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=3)
    m = multiprocessing.Manager()
    q = m.Queue(maxsize=10)

    pool.apply_async(worker, (33, q))
    pool.apply_async(worker, (40, q))
    pool.apply_async(worker, (27, q))

    while True:
    try:
    print(q.get(False))
    except:
    pass

    Output:


    27 is done
    33 is done
    40 is done
    ​
    Not Working OOP Code:


    import multiprocessing

    class Workers:
    def __init__(self):
    self.pool = multiprocessing.Pool(processes=3)
    self.m = multiprocessing.Manager()
    self.q = self.m.Queue()

    self.pool.apply_async(self.worker, (33, self.q))
    self.pool.apply_async(self.worker, (40, self.q))
    self.pool.apply_async(self.worker, (27, self.q))

    while True:
    try:
    print(self.q.get(False))
    except:
    pass

    def worker(self, name, que):
    que.put("%d is done" % name)


    if __name__ == "__main__":
    w = Workers()

    Output:

    Continue reading...

Compartilhe esta Página