sharebook-kr / pyupbit

python wrapper for upbit API
Apache License 2.0
482 stars 311 forks source link

websocket 이 연결이 끊어지네요 #19

Open pcompassion opened 3 years ago

pcompassion commented 3 years ago
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/pyupbit/websocket_api.py", line 54, in run
    self.__aloop.run_until_complete(self.__connect_socket())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/pyupbit/websocket_api.py", line 48, in __connect_socket
    recv_data = await websocket.recv()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/websockets/protocol.py", line 509, in recv                                                 await self.ensure_open()                                                                                                                                      File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/websockets/protocol.py", line 803, in ensure_open                                          raise self.connection_closed_exc()                                                                                                                          websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
PYTHONKOR commented 3 years ago

질문에 영양가 없는 댓글이라 삭제합니다. 😂

Codejune commented 3 years ago

웹소켓의 경우 업비트 서버와 클라이언트(본인) 간의 데이터 교류가 없을경우 임의적으로 중단하는 현상이 발생합니다. 업비트 api문서에 따르면 ping-pong을 지원하니 이를 반영하는 것이 좋을 것 같습니다

mr-yoo commented 3 years ago

@Codejune 님 말씀이 맞습니다.

서버에서는 기본적으로 아무런 데이터도 수/발신 되지 않은 채 약 120초가 경과하면 Idle Timeout으로 WebSocket Connection을 종료합니다. 이를 방지하기 위해 클라이언트에서 서버로 PING 메시지를 보내서 Connection을 유지하고, WebSocket 서버의 상태와 WebSocket Connection Status를 파악할 수 있습니다. reference : https://docs.upbit.com/docs/upbit-quotation-websocket#client-to-server-ping

mr-yoo commented 3 years ago

다음 commit에서 ping interval을 60s로 설정을 했습니다. https://github.com/sharebook-kr/pyupbit/commit/396877399b1078c183d691cba83eb591b9fa9a2d

async with websockets.connect(uri, ping_interval=60) as websocket:
    data = [{"ticket": str(uuid.uuid4())[:6]}, {"type": self.type, "codes": self.codes}]
    await websocket.send(json.dumps(data))

문제가 재현 된다면 알려주세요.

littlehome-eugene commented 3 years ago

좀 다른 에러가 나네요 이제 (ping 60s 적용코드 사용중입니다)

Process WebSocketManager-1:
Traceback (most recent call last):
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/websockets/protocol.py", line 838, in transfer_data
    await asyncio.shield(self._put_message_waiter)
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/pyupbit/websocket_api.py", line 54, in run
    self.__aloop.run_until_complete(self.__connect_socket())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/pyupbit/websocket_api.py", line 48, in __connect_socket
    recv_data = await websocket.recv()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/websockets/protocol.py", line 509, in recv
    await self.ensure_open()
  File "/home/eugenekim/virtualenvs/snowball/lib/python3.8/site-packages/websockets/protocol.py", line 803, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason