Python asyncio拒绝承认新的事件循环

时间:2018-06-01 06:18:22

标签: python python-3.x python-asyncio

这是我第一次在Python上使用asyncio,请耐心等待。我有一个集成异步的for循环,而似乎对我来说非常合乎逻辑显然是完全错误的。

在循环结束时,我关闭循环,然后创建并设置一个新循环以继续脚本。但是,我不断得到“事件循环已关闭”运行时错误 - 但不是所有时间,可能是大约2/3的时间。

我在这里缺少什么?

try:
    for data in targetData:
        loop = asyncio.get_event_loop()
        graph = loop.run_until_complete(load_graph(data))
        paths = bellman_ford(graph, unique_paths=True)

        for path in paths:
            try:
                print_opportunity_for_path(graph, path)
            except Exception as exc:
                print ('Error: {}'.format(exc))

        asyncio.wait(1)
        loop.close()
        newLoop = asyncio.new_event_loop()
        assert(loop != newLoop)
        asyncio.set_event_loop(newLoop)

这让我感到疯狂:)我甚至已经离开了添加随机等待并且可能是不必要的断言。

编辑:看起来像是导致头痛的第三方库..?

    Exception ignored in: <bound method Exchange.__del__ of <ccxt.async.gdax.gdax object at 0x7fe09c07b0f0>>
    Traceback (most recent call last):
      File "/home/hyoon/scripts/arb/local/lib/python3.6/site-packages/ccxt/async/base/exchange.py", line 64, in __del__
        self.asyncio_loop.run_until_complete(self.session.close())
      File "/usr/lib/python3.6/asyncio/base_events.py", line 444, in run_until_complete
        self._check_closed()
      File "/usr/lib/python3.6/asyncio/base_events.py", line 358, in _check_closed
        raise RuntimeError('Event loop is closed')
    RuntimeError: Event loop is closed
    /home/hyoon/scripts/arb/local/lib/python3.6/site-packages/peregrinearb/utils/data_structures.py:62: RuntimeWarning: coroutine 'ClientSession.close' was never awaited
      heapq.heappush(self.heap, (pri, d))
    Unclosed client session
    client_session: <aiohttp.client.ClientSession object at 0x7fe09c07b400>
    Unclosed connector
    connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7fe0a79cc160>, 696463.579907268), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6898>, 696463.613516469), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea69b0>, 696463.615550519), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6a20>, 696463.616189468), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09af0a630>, 696463.616714588), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6940>, 696463.621970396), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6860>, 696463.622567727), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6908>, 696463.624466974), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6978>, 696463.625720629), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea68d0>, 696463.630833432), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea69e8>, 696463.635552591), (<aiohttp.client_proto.ResponseHandler object at 0x7fe09aea6a58>, 696463.639611507)]']
    connector: <aiohttp.connector.TCPConnector object at 0x7fe09c07b390>
    Fatal write error on socket transport
    protocol: <asyncio.sslproto.SSLProtocol object at 0x7fe09aea6ba8>
    transport: <_SelectorSocketTransport fd=7>
    Traceback (most recent call last):
      File "/usr/lib/python3.6/asyncio/selector_events.py", line 761, in write
        n = self._sock.send(data)
    OSError: [Errno 9] Bad file descriptor
    Fatal error on SSL transport
    protocol: <asyncio.sslproto.SSLProtocol object at 0x7fe09aea6ba8>
    transport: <_SelectorSocketTransport closing fd=7>
    Traceback (most recent call last):
      File "/usr/lib/python3.6/asyncio/selector_events.py", line 761, in write
        n = self._sock.send(data)
    OSError: [Errno 9] Bad file descriptor

0 个答案:

没有答案