Skip to content

Instantly share code, notes, and snippets.

@iissme
iissme / sync_call.py
Created May 31, 2017 11:39 — forked from sethrh/sync_call.py
Trick to synchronously call a coroutine from a non-asyncio-aware legacy code. The calling thread can treat the coroutine as a regular (synchronous) function call.
#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3
"""
Synchronously call an asyncio task/coroutine from a non-asyncio thread.
usage:
# block the current thread until the coroutine yields or returns
value = sync_call(loop, coroutine, 1, 2, 3, four=4, five=5)
# control returns here when the coroutine is done
"""
import asyncio