How to use Queue in concurrent.futures.ProcessPoolExecutor()?

断了今生、忘了曾经 提交于 2019-12-24 04:12:09

问题


Disclaimer: I'm new to Python in general. I have a small experience with Go, where implementing a queue using a channel is really easy.

I want to know how can I implement a Queue with ProcessPoolExecutor in Python 3.

I want my N number of process to access a single queue so that I can just insert many jobs in the queue via the main thread, then the processes will just grab the jobs in the Queue.

Or if there is a better way to share a list/queue between multiple processes. (Job Queue/ Worker Pool maybe ?)

Thanks.


回答1:


concurrent.futures does this for you. The executor object implements a queue internally, so when you submit tasks, they get put into the queue and your worker threads or worker processes pick jobs up and run them.

It may feel as though it's "too easy", but that's what concurrent.futures is all about - abstracting away all the complexity of managing threadpools or processpools, job queues, etc. so you can trade a little overhead for a lot of saved time and effort.

Here's what it looks like:

import concurrent.futures

def send_email(from, to, subject, message):
    # magic to send an email

executor = concurrent.futures.ProcessPoolExecutor()
future = executor.submit(send_email, 'me@example.com', 'you@example.com', 'Hi!', 'Nice to meet you')

That one simple submit call takes your function and its arguments, wraps them into a work item, puts them into a queue and the process pool that was initialised when you created your executor will pick off the jobs and run them.



来源:https://stackoverflow.com/questions/52478532/how-to-use-queue-in-concurrent-futures-processpoolexecutor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!