2017-11-03 12 views
2

私はFedora 26 WorkstationでPython 3.6.2を使用しています。asyncio:新しいイベントループを作成できません

EDIT:以下

は私の問題を示し、いくつかのスクラップブックのコードであるコードにサム・ハートマンの提案を追加しました。初期asyncioイベントループ、別のものを作成し、グローバルイベントループとして設定を閉じた後

import asyncio, json 
from autobahn.asyncio.websocket import WebSocketClientProtocol, WebSocketClientFactory 

class MyClientProtocol(WebSocketClientProtocol): 

    def onConnect(self, response): 
     print(response.peer) 

    def onOpen(self): 
     print("open") 
     self.sendMessage(json.dumps({'command': 'subscribe', 'channel': "1010"}).encode("utf8")) 

    def onMessage(self, payload, isBinary): 
     print("message") 
     print(json.loads(payload)) 

factory1 = WebSocketClientFactory("wss://api2.poloniex.com:443") 
factory1.protocol = MyClientProtocol 
loop1 = asyncio.get_event_loop() 
loop1.run_until_complete(loop1.create_connection(factory1, "api2.poloniex.com", 443, ssl=True)) 

try: 
    loop1.run_forever() 
except KeyboardInterrupt: 
    pass 
loop1.close() 

asyncio.set_event_loop(asyncio.new_event_loop()) 

factory2 = WebSocketClientFactory("wss://api2.poloniex.com:443") 
factory2.protocol = MyClientProtocol 
loop2 = asyncio.get_event_loop() 
loop2.run_until_complete(loop2.create_connection(factory2, "api2.poloniex.com", 443, ssl=True)) 

try: 
    loop2.run_forever() 
except KeyboardInterrupt: 
    pass 
loop2.close() 

、新しいイベントループを使用しようとすると、以下のエラーが出力されます

Fatal write error on socket transport 
protocol: <asyncio.sslproto.SSLProtocol object at 0x7f8a84ed4748> 
transport: <_SelectorSocketTransport fd=6> 
Traceback (most recent call last): 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 762, in write 
    n = self._sock.send(data) 
OSError: [Errno 9] Bad file descriptor 
Fatal error on SSL transport 
protocol: <asyncio.sslproto.SSLProtocol object at 0x7f8a84ed4748> 
transport: <_SelectorSocketTransport closing fd=6> 
Traceback (most recent call last): 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 762, in write 
    n = self._sock.send(data) 
OSError: [Errno 9] Bad file descriptor 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/usr/lib64/python3.6/asyncio/sslproto.py", line 648, in _process_write_backlog 
    self._transport.write(chunk) 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 766, in write 
    self._fatal_error(exc, 'Fatal write error on socket transport') 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 646, in _fatal_error 
    self._force_close(exc) 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 658, in _force_close 
    self._loop.call_soon(self._call_connection_lost, exc) 
    File "/usr/lib64/python3.6/asyncio/base_events.py", line 574, in call_soon 
    self._check_closed() 
    File "/usr/lib64/python3.6/asyncio/base_events.py", line 357, in _check_closed 
    raise RuntimeError('Event loop is closed') 
RuntimeError: Event loop is closed 
Exception in callback _SelectorSocketTransport._read_ready() 
handle: <Handle _SelectorSocketTransport._read_ready()> 
Traceback (most recent call last): 
    File "/usr/lib/python3.6/site-packages/txaio/_common.py", line 63, in call_later 
    self._buckets[real_time][1].append(call) 
KeyError: 412835000 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/usr/lib64/python3.6/asyncio/events.py", line 127, in _run 
    self._callback(*self._args) 
    File "/usr/lib64/python3.6/asyncio/selector_events.py", line 731, in _read_ready 
    self._protocol.data_received(data) 
    File "/usr/lib64/python3.6/asyncio/sslproto.py", line 503, in data_received 
    ssldata, appdata = self._sslpipe.feed_ssldata(data) 
    File "/usr/lib64/python3.6/asyncio/sslproto.py", line 204, in feed_ssldata 
    self._handshake_cb(None) 
    File "/usr/lib64/python3.6/asyncio/sslproto.py", line 619, in _on_handshake_complete 
    self._app_protocol.connection_made(self._app_transport) 
    File "/usr/lib/python3.6/site-packages/autobahn/asyncio/websocket.py", line 97, in connection_made 
    self._connectionMade() 
    File "/usr/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 3340, in _connectionMade 
    WebSocketProtocol._connectionMade(self) 
    File "/usr/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 1055, in _connectionMade 
    self.onOpenHandshakeTimeout, 
    File "/usr/lib/python3.6/site-packages/txaio/_common.py", line 72, in call_later 
    self._notify_bucket, real_time, 
    File "/usr/lib/python3.6/site-packages/txaio/aio.py", line 382, in call_later 
    return self._config.loop.call_later(delay, real_call) 
    File "/usr/lib64/python3.6/asyncio/base_events.py", line 543, in call_later 
    timer = self.call_at(self.time() + delay, callback, *args) 
    File "/usr/lib64/python3.6/asyncio/base_events.py", line 553, in call_at 
    self._check_closed() 
    File "/usr/lib64/python3.6/asyncio/base_events.py", line 357, in _check_closed 
    raise RuntimeError('Event loop is closed') 
RuntimeError: Event loop is closed 

これは、という合理的なようです以前のイベントループを閉じた後にイベントループを再度開く必要があるかもしれません。確かにこの質問をしても方法を示しています。以下Asyncio Event Loop is Closed

コードは、これを達成する必要があります

loop = asyncio.new_event_loop() 
asyncio.set_event_loop(loop) 

ので、私は明らかに間違って何かをやっています。誰かが行方不明を見ることができますか?

+0

を閉じた後、ウェブソケットファクトリを再構築するのですか? –

+0

イベントループは 'loop.close()'と呼ばれるポイントで実行されていないので、何もしません。 –

答えて

1

私は、あなたの工場オブジェクトがおそらくasyncio.get_event_loopから得られる古いイベントループへの参照を維持しているというかなり高い確信を持っています。 Asyncioの消費者は、ループへの隠れた参照を取得することに悪いです。

私の推薦は、あなたが `loop.stop()`の代わりに `クローズを()`で実行しようとしたことがありループ

+0

ありがとうSam。私はファクトリを再構築しようとしました、最初と2つ目のファクトリ/ループには異なる変数名を使用していましたが、私はまだ同じエラーを受けています( 'OSError:[Errno 9]ファイル記述子が間違っています'、 'RuntimeError: 'KeyError:15068000')。あたかも 'set_event_loop'呼び出しが単に機能していないかのようです。私はこれをバグとして報告すべきだと思いますか? –

+1

get_event_loopが設定した内容を返すと、set_event_loopがアサートと連動していることを確認できます。おそらくWebSocket実装の –

+0

でもおそらくどこかで参照がぶら下がっています。私はautobahn.asyncioの人々に尋ねるでしょう。おかげさまでサム。 –

関連する問題