2017-11-03 2 views
2

Fedora 26 Workstation에서 Python 3.6.2를 사용하고 있습니다.asyncio : 새 이벤트 루프를 만들 수 없습니다.

편집 : 아래

내 문제를 보여줍니다 일부 스크랩북 코드 코드 샘 하트 맨의 제안을 추가했습니다.

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 

그것은 합리적인 것 같다

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

한 후 다음과 같은 오류가 새 이벤트 루프를 사용하여 산출 시도, 서로를 생성하고 글로벌 이벤트 루프로 설정, 초기 asyncio 이벤트 루프를 폐쇄 이전 이벤트 루프를 닫은 후 이벤트 루프를 다시 열어야 할 수도 있습니다. Asyncio Event Loop is Closed

아래 코드는 이것을 달성한다 : 사실조차이 문제는 방법을 보여줍니다 그래서 분명 뭔가 잘못하고있는 중이 야

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

합니다. 누군가 실종 된 것을 볼 수 있습니까?

+0

당신이 봤어 루프 실행'loop.stop()를 닫은 후 웹 소켓 팩토리를 재구성하는 것입니다? –

+0

이벤트 루프는 'loop.close()'를 호출하는 지점에서 실행되지 않으므로 아무런 작동도하지 않습니다. –

답변

1

공장 개체가 이전 이벤트 루프 (예 : asyncio.get_event_loop)의 참조를 유지하고 있음을 확실히 확신 할 수 있습니다. Asyncio 소비자는 루프에 대한 숨겨진 참조를 가져 오는 것이 좋지 않습니다.

나의 추천`대신`가까운()`의

+0

감사 샘. 나는 공장을 재구성하려고 시도했다. 첫 번째와 두 번째 팩토리/루프에 다른 변수 이름을 사용 했는데도 여전히 동일한 오류가 발생했다. (OSError : [Errno 9] 잘못된 파일 기술자, RuntimeError : 이벤트 루프가 닫혔다. 'KeyError : 15068000'). 마치'set_event_loop' 호출이 단순히 작동하지 않는 것과 같습니다. 내가 이것을 벌레로보고해야한다고 생각하니? –

+1

get_event_loop이 설정 한 값을 반환 한 후에 set_event_loop이 assert로 작업하고 있는지 확인할 수 있습니다. 아마도 어딘가에 참조가 매달려있을 것입니다. 아마도 웹 소켓 구현 –

+0

에서 OK입니다. 나는 autobahn.asyncio 사람들에게 물어볼 것이다. 감사 샘. –

관련 문제