Await Future from Executor: Future can't be used in 'await' expression
You should use loop.run_in_executor
:
from concurrent.futures import ThreadPoolExecutorimport asynciodef work(): # do some blocking io passasync def main(loop): executor = ThreadPoolExecutor() await loop.run_in_executor(executor, work)loop = asyncio.get_event_loop()loop.run_until_complete(main(loop))loop.close()
EDIT
concurrent.futures.Future
object are different from asyncio.Future
. The asyncio.Future
is intended to be used with event loops and is awaitable, while the former isn't. loop.run_in_executor
provides the necessary interoperability between the two.
EDIT #2
Using executor.submit is not my decision. This is used internally by several libraries, like requests-futures. I am searching for a way to interop with those modules from coroutines.
EDIT #3
Since python 3.5 (docs), you can use asyncio.wrap_future(future, *, loop=None)
to convert a concurrent.futures.Future
to a asyncio.Future
.