Skip to content

Commit 7133688

Browse files
author
clickingbuttons
authored
Fixes by at-cf (#297)
* Fixes by at-cf * fix lint
1 parent fbaa3e6 commit 7133688

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

examples/websocket/latency.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from polygon import WebSocketClient
2+
from polygon.websocket.models import WebSocketMessage, EquityQuote
3+
from typing import List, cast
4+
import time
5+
6+
c = WebSocketClient(subscriptions=["Q.SPY"])
7+
8+
9+
def handle_msg(msgs: List[WebSocketMessage]):
10+
for m in msgs:
11+
q: EquityQuote = cast(EquityQuote, m)
12+
if q.timestamp is not None:
13+
now = time.time() * 1000
14+
print(now, q.timestamp, now - q.timestamp)
15+
16+
17+
c.run(handle_msg)

polygon/websocket/__init__.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,20 @@ async def connect(
118118
self.subs = set(self.scheduled_subs)
119119
self.schedule_resub = False
120120

121-
cmsg: Union[
122-
List[WebSocketMessage], Union[str, bytes]
123-
] = await s.recv()
124-
# we know cmsg is Data
125-
msgJson = json.loads(cmsg) # type: ignore
126-
for m in msgJson:
127-
if m["ev"] == "status":
128-
logger.debug("status: %s", m["message"])
129-
continue
121+
try:
122+
cmsg: Union[
123+
List[WebSocketMessage], Union[str, bytes]
124+
] = await asyncio.wait_for(s.recv(), timeout=1)
125+
except asyncio.TimeoutError:
126+
continue
127+
130128
if not self.raw:
129+
# we know cmsg is Data
130+
msgJson = json.loads(cmsg) # type: ignore
131+
for m in msgJson:
132+
if m["ev"] == "status":
133+
logger.debug("status: %s", m["message"])
134+
continue
131135
cmsg = parse(msgJson, logger)
132136

133137
if len(cmsg) > 0:

0 commit comments

Comments
 (0)