Why is `multiprocessing.Queue.get` so slow?

前端 未结 2 558
再見小時候
再見小時候 2021-02-07 09:37

I need help in understanding multiprocessing.Queue. The problem I\'m facing is that getting results from queue.get(...) are hilariously behind compared

2条回答
  •  北海茫月
    2021-02-07 09:52

    For future readers, you could also try using:

    q = multiprocessing.Manager().Queue()
    

    Instead of just

    q = multiprocessing.Queue()
    

    I haven't yet fully distilled and understood the mechanisms behind this behavior, but one source I've read claimed it's about:

    "when pushing large items onto the queue, the items are essentially buffered, despite the immediate return of the queue’s put function."

    The author goes on explaining more about it and a way to fix, but for me, adding the Manager did the trick easy and clean.

    UPDATE: I believe this StackOverflow answer is helpful in explaining the issue.

    FMQ, mentioned in the accepted answer, is also Python2 exclusive, which is one of the reasons I felt this answer could maybe help more people someday.

提交回复
热议问题