我有一个名为
问题在于,由
我还注意到生成器不可序列化,因此将协程提交给
long_task
的函数,它运行重型的CPU计算。我想通过使用新的asyncio框架将其异步化。生成的long_task_async
函数使用ProcessPoolExecutor
将工作转移到不受GIL约束的不同进程中。问题在于,由
ProcessPoolExecutor.submit
返回的concurrent.futures.Future
实例在被yield时会抛出TypeError
异常。这是设计上的问题吗?这些future是否与asyncio.Future
类不兼容?有什么解决方法?我还注意到生成器不可序列化,因此将协程提交给
ProcessPoolExecutor
将失败。是否有任何干净的解决方案?import asyncio
from concurrent.futures import ProcessPoolExecutor
@asyncio.coroutine
def long_task():
yield from asyncio.sleep(4)
return "completed"
@asyncio.coroutine
def long_task_async():
with ProcessPoolExecutor(1) as ex:
return (yield from ex.submit(long_task)) #TypeError: 'Future' object is not iterable
# long_task is a generator, can't be pickled
loop = asyncio.get_event_loop()
@asyncio.coroutine
def main():
n = yield from long_task_async()
print( n )
loop.run_until_complete(main())