Skip to content

Commit

Permalink
Fixes by at-cf (#297)
Browse files Browse the repository at this point in the history
* Fixes by at-cf

* fix lint
  • Loading branch information
clickingbuttons authored Sep 9, 2022
1 parent fbaa3e6 commit 7133688
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 17 additions & 0 deletions examples/websocket/latency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage, EquityQuote
from typing import List, cast
import time

c = WebSocketClient(subscriptions=["Q.SPY"])


def handle_msg(msgs: List[WebSocketMessage]):
for m in msgs:
q: EquityQuote = cast(EquityQuote, m)
if q.timestamp is not None:
now = time.time() * 1000
print(now, q.timestamp, now - q.timestamp)


c.run(handle_msg)
22 changes: 13 additions & 9 deletions polygon/websocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,20 @@ async def connect(
self.subs = set(self.scheduled_subs)
self.schedule_resub = False

cmsg: Union[
List[WebSocketMessage], Union[str, bytes]
] = await s.recv()
# we know cmsg is Data
msgJson = json.loads(cmsg) # type: ignore
for m in msgJson:
if m["ev"] == "status":
logger.debug("status: %s", m["message"])
continue
try:
cmsg: Union[
List[WebSocketMessage], Union[str, bytes]
] = await asyncio.wait_for(s.recv(), timeout=1)
except asyncio.TimeoutError:
continue

if not self.raw:
# we know cmsg is Data
msgJson = json.loads(cmsg) # type: ignore
for m in msgJson:
if m["ev"] == "status":
logger.debug("status: %s", m["message"])
continue
cmsg = parse(msgJson, logger)

if len(cmsg) > 0:
Expand Down

0 comments on commit 7133688

Please sign in to comment.