GeezerVison is showing github.com — Fixes by at-cf (#297) · riddopic/client-python@7133688
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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) |
Collapse file
Copy file name to clipboardExpand all lines: polygon/websocket/__init__.py +13-9Lines changed: 13 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -118,16 +118,20 @@ async def connect( |
118 | 118 | self.subs = set(self.scheduled_subs) |
119 | 119 | self.schedule_resub = False |
120 | 120 |
|
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 | + |
130 | 128 | 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 |
131 | 135 | cmsg = parse(msgJson, logger) |
132 | 136 |
|
133 | 137 | if len(cmsg) > 0: |
|