Forwarded from 依云
asyncio 下,一大堆任务怎么处理比较好呢?一个任务队列一个结果队列、若干个 worker?
Forwarded from 依云
我之前是这么干的:
async def main(proto):
q = asyncio.Queue()
ret_q = asyncio.Queue()
futures = [worker(q, ret_q) for _ in range(40)]
producer_fu = asyncio.ensure_future(producer(q, proto))
printer_fu = asyncio.ensure_future(printer(ret_q))
await asyncio.wait(futures)
printer_fu.cancel()
await producer_fu
await printer_fu
Forwarded from 依云
一个 coroutine 往 q 里放任务,一个 coroutine 从 ret_q 里取任务,再加上一堆 worker
Forwarded from Hung-I Wang
如果你不加任何 synchronization primitives ,只有一个 while loop + 变量判断,在 asyncio 下没有显示的 yield point,会直接卡死,其它任务不会有执行机会。
Forwarded from Hung-I Wang
不用 Semaphore 这样的 synchronization primitives 没法写。
依云
我之前是这么干的: async def main(proto): q = asyncio.Queue() ret_q = asyncio.Queue() futures = [worker(q, ret_q) for _ in range(40)] producer_fu = asyncio.ensure_future(producer(q, proto)) printer_fu = asyncio.ensure_future(printer(ret_q)) await as…
import asyncio
from asyncio import ensure_future
async def main(proto, n_parallelism = 40):
q_arg = asyncio.Queue()
q_res = asyncio.Queue()
futures = [worker(q_arg, q_res) for _ in range(n_parallelism)]
producer = ensure_future(producer(q_arg, proto))
consumer = ensure_future(consumer(q_res))
await asyncio.wait(futures)
consumer.cancel() #🤔
await producer
await consumer (刚才还在想
q_arg 和 q_res 哪来的联系,发现自己记漏了 worker(q_arg, q_res)Forwarded from 天天向上 (Basic I/O Interface)
The latest version of CMake (3.17.1) was released.