我的问题非常类似于Combining asyncio with a multi-worker ProcessPoolExecutor - 但是一个细微的变化(我相信是async for)使得那里的优秀答案对我来说不可用。
我正在尝试以下 MWE:
import concurrent.futures
import asyncio
import time
async def mygen(u: int = 2):
i = 0
while i < u:
yield i
i += 1
def blocking(delay):
time.sleep(delay+1)
return('EXECUTOR: Completed blocking task number ' + str(delay+1))
async def non_blocking(loop):
with concurrent.futures.ProcessPoolExecutor() as executor:
async for i in mygen():
print('MASTER: Sending to executor blocking task number ' + str(i+1))
result = await loop.run_in_executor(executor, blocking, i)
print(result)
print('MASTER: Well …Run Code Online (Sandbox Code Playgroud)