I am trying to receive live ticks from binance through websocket module and using event (queue) to share the tick data with other libraries. My main file look like this:
<main.py>
from stream import StreamingForexPrices as SF
import threading
import time
import queue
events = queue.Queue()
prices = SF(events)
wst = threading.Thread(target=prices.ws.run_forever, args=[])
wst.daemon = True
wst.start()
while not prices.ws.sock.connected:
time.sleep(1)
print("checking1111111")
print("this", prices.events_queue.get()) ###<<< My code Stuck Here
# conn_timeout -= 1
while prices.ws.sock is not None:
# print(prices.ws.sock)
print("this", prices.events_queue.get())
print("checking2222222")
time.sleep(10)
My code just run an infinite loop while not printing any values inside the queue and not showing any error messages in the kernal. I just want to confirm that my program receive the ticks and puts it to the inside the queue for further processing. To reproduce my issue this is the stream module:
from __future__ import print_function
from datetime import datetime
import json, websocket, time
from event import TickEvent
class StreamingForexPrices:
def __init__(
self, events_queue
):
self.events_queue = events_queue
self.prices = dict()
self.websocket = websocket
self.socket = f'wss://stream.binance.com:9443/ws/btcusdt@ticker/ethbtc@ticker/bnbbtc@ticker/wavesbtc@ticker/stratbtc@ticker/ethup@ticker/yfiup@ticker/xrpup@ticker'
self.websocket.enableTrace(False)
self.ws = self.websocket.WebSocketApp(
self.socket, on_message=self.on_message, on_close=self.on_close)
def on_close(self, ws, message):
print("bang")
def on_message(self, ws, message):
data = json.loads(message)
self.timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')
self.instrument = data['s']
open = data['o']
high = data['h']
low = data['l']
close = data['c']
volume = data['v']
trade = data['n'] #No. of Trades
# print("testing")
self.prices[self.instrument]["open"] = open
self.prices[self.instrument]["high"] = high
self.prices[self.instrument]["low"] = low
self.prices[self.instrument]["close"] = close
self.prices[self.instrument]["volume"] = volume
self.prices[self.instrument]["trade"] = trade
tev = TickEvent(self.instrument, self.timestamp, open, high, low, close, volume, trade)
self.events_queue.put(tev)
You're going to hate this answer.
The PROBLEM here is this statement:
self.prices[self.instrument]["open"] = open
Why is this a problem? Because self.instrument
does not exist as a key in the self.prices
dictionary. That causes a KeyError, so nothing ever gets stored in your queue. Normally, you would have received a KeyError that would have told you where the problem was immediately, but the websocket
module calls your code in a try/except
block that suppresses the errors. You MIGHT consider using your own try/except
block and doing your own traceback.
If you add this just before that line:
if self.instrument not in self.prices:
self.prices[self.instrument] = {}
then your code all works as you expect.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.