重い CPU バウンドの計算を実行するlong_task
関数があり、新しい asyncio フレームワークを使用して非同期にしたいと考えています。結果のlong_task_async
関数はProcessPoolExecutor
、GIL によって制約されないように作業を別のプロセスにオフロードするために a を使用します。
問題は、何らかの理由でconcurrent.futures.Future
from から返されたときに返されたインスタンスProcessPoolExecutor.submit
が をスローすることTypeError
です。これは設計によるものですか?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())