队列中的asyncio执行器

时间:2017-02-14 11:45:23

标签: python-3.x queue executor

我很难确定从队列实现异步执行的正确模式。看起来我不能在队列get循环中使用池中的空闲执行程序,直到所有这些都完成。

MAX_WORKERS = 10
queue = queue.Queue()
stop_event = threading.Event()

def post(data):
    requests.post('http://127.0.0.1', data)

async def poster():
    loop = asyncio.get_event_loop()
    while not stop_event.isSet():
        futures = set()

        while not stop_event.isSet() and len(futures) < MAX_WORKERS:
            data = queue.get()
            if data is None:
                continue
            try:
                future = loop.run_in_executor(None, post, data)
            except:
                continue
            futures.add(future)
            queue.task_done()

        gathered_future = asyncio.gather(*futures, return_exceptions=True)

        if not stop_event.isSet():
            await gathered_future
        else:
            gathered_future.cancel()

executor = ProcessPoolExecutor(max_workers=MAX_WORKERS)
loop.set_default_executor(executor)
try:
    loop.run_until_complete(poster())
finally:
    loop.close()

0 个答案:

没有答案