【问题标题】:What can cause the simple invocation of asyncio.new_event_loop() to hang?什么会导致 asyncio.new_event_loop() 的简单调用挂起?
【发布时间】:2016-06-22 00:46:40
【问题描述】:

我正在使用以下函数来强制协程同步运行:

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

但是,它会间歇性地在尝试创建新循环时冻结:loop = asyncio.new_event_loop()。检查堆栈跟踪显示了它挂起的确切位置:

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()

在像socket 这样低级别的库中,什么会导致这样的问题?难道我做错了什么?我正在使用 Python 3.5.1。

编辑:我提交了错误报告 here,但 Guido 建议我继续在 StackOverflow 上寻求帮助。

【问题讨论】:

  • 这根本不是重复的。我的代码在 asyncio.new_event_loop 的简单调用上挂起,另一个问题是关于在创建后挂起的循环。
  • 小提示:在 is_awaitable 中,您正在检查 futures.Future 的 isinstance。如果我是对的,那就错了:你应该检查asyncio.Future。这些类是不同的。
  • @ErwinMayer: File: "\lib\asyncio\selector_events.py", line 116, in _make_self_pipe
  • 它不会在 python 3.7.0 上冻结,也可以在 3.6.1 上运行。

标签: python freeze coroutine python-asyncio event-loop


【解决方案1】:

我正在尝试了解您要做什么。

如果我理解正确,您需要一个无论输入是协程还是简单函数调用都将返回相同内容的函数。如果我是正确的,那么这似乎工作正常。

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)))

这会输出以下(我猜是预期的)结果:

xy2
xy3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多