File tree Expand file tree Collapse file tree 2 files changed +30
-9
lines changed
Expand file tree Collapse file tree 2 files changed +30
-9
lines changed 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 )
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments