I am using the following function to force a coroutine to run synchronously:
import asyncio
import inspect
import types
from asyncio import BaseEventLoop
from concurrent import futures
def await_sync(coro: types.CoroutineType, timeout_s: int=None):
"""
:param coro: a coroutine or lambda loop: coroutine(loop)
:param timeout_s:
:return:
"""
loop = asyncio.new_event_loop() # type: BaseEventLoop
if not is_awaitable(coro):
coro = coro(loop)
if timeout_s is None:
fut = asyncio.ensure_future(coro, loop=loop)
else:
fut = asyncio.ensure_future(asyncio.wait_for(coro, timeout=timeout_s, loop=loop), loop=loop)
loop.run_until_complete(fut)
return fut.result()
def is_awaitable(coro_or_future):
if isinstance(coro_or_future, futures.Future):
return coro_or_future
elif asyncio.coroutines.iscoroutine(coro_or_future):
return True
elif asyncio.compat.PY35 and inspect.isawaitable(coro_or_future):
return True
else:
return False
However, intermittently, it will freeze upon simply trying to create a new loop: loop = asyncio.new_event_loop()
. Inspecting the stack traces shows me the exact location where it hangs:
File: "/src\system\utils.py", line 34, in await_sync
loop = asyncio.new_event_loop() # type: BaseEventLoop
File: "\lib\asyncio\events.py", line 636, in new_event_loop
return get_event_loop_policy().new_event_loop()
File: "\lib\asyncio\events.py", line 587, in new_event_loop
return self._loop_factory()
File: "\lib\asyncio\selector_events.py", line 55, in __init__
self._make_self_pipe()
File: "\lib\asyncio\selector_events.py", line 116, in _make_self_pipe
self._ssock, self._csock = self._socketpair()
File: "\lib\asyncio\windows_events.py", line 295, in _socketpair
return windows_utils.socketpair()
File: "\lib\socket.py", line 515, in socketpair
ssock, _ = lsock.accept()
File: "\lib\socket.py", line 195, in accept
fd, addr = self._accept()
What can be causing such an issue, in a library as low level as socket
? Am I doing something wrong? I am using Python 3.5.1.
Edit: I filed a bug report here but Guido recommended me to continue seeking help on StackOverflow.
It should be used as a main entry point for asyncio programs, and should ideally only be called once. New in version 3.7.
This function can only be called from a coroutine or a callback. New in version 3.7. Get the current event loop. If there is no current event loop set in the current OS thread, the OS thread is main, and set_event_loop() has not yet been called, asyncio will create a new event loop and set it as the current one.
Event loop is the core of Python asyncio . Every coroutine , Future , or Task would be scheduled as callback and be executed by an event loop. In this blog post, I would like to look into Python event loop at the low-level implementation superficially.
asyncio is a library to write concurrent code using the async/await syntax. asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc.
I'm trying to understand what you are trying to do.
If I get it correctly you want a function that will return the same thing whether the input is a coroutine or a simple function call. If I'm correct, then this seems to work fine.
import asyncio
import time
def await_sync(coro, timeout=None):
if asyncio.iscoroutine(coro):
f = asyncio.wait_for(coro, timeout)
loop = asyncio.get_event_loop()
return loop.run_until_complete(f)
return coro
async def async_test(x):
print("x", end="")
await asyncio.sleep(x)
print("y", end="")
return x
def sync_test(x):
print("x", end="")
time.sleep(x)
print("y", end="")
return x
print(await_sync(sync_test(2)))
print(await_sync(async_test(3)))
This outputs the following (I guess expected) result:
xy2
xy3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With