Thursday 29 July 2021

Calling queue variable processed outside of __init__ getting Empty values in python

I have made a python script to receive live ticks and put it inside a queue for further processing but my problem is that as I have defined the queue variable in the class __init__ method and putting the received ticks by calling another function inside the same class but when calling it from another function it gets the variable from __init__ and not directly from other function where I put the values into the queue it is getting queue.empty error. Edit: "If you have any suggestion to improve my question for a better understanding you are welcome."

My code:

main.py:

from stream import StreamingForexPrices as SF
from threading import Thread, Event
import time
from queue import Queue, Empty

def fetch_data(data_q):
    while True:
        time.sleep(10)
        # data_q.put("checking")
        data = data_q.get(False)
        print(data)

def start():
    events = Queue()

    fetch_thread = Thread(target=fetch_data, args=(events,))
    fetch_thread.daemon = True
    fetch_thread.start()

    prices = SF(events)
    wst = Thread(target=prices.conn)
    wst.daemon = True
    wst.start()
    while not prices.ws.sock.connected:
        time.sleep(1)
        print("checking1111111")
    while prices.ws.sock is not None:
        print("checking2222222")
        time.sleep(10)
if __name__ == "__main__":
    start()

stream.py:

from __future__ import print_function
from datetime import datetime
import json, websocket, time
from event import TickEvent

class StreamingForexPrices(object):

    def __init__(
        self, events_queue
    ):
        self.events_queue = events_queue
        # self.conn()

    def conn(self):
        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'
        websocket.enableTrace(False)
        self.ws = websocket.WebSocketApp(
            self.socket, on_message=self.on_message, on_close=self.on_close)
        self.ws.run_forever()
   
    def on_close(self, ws, message):
        print("bang")

    def on_message(self, ws, message):
        data = json.loads(message)
        timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')
        instrument = data['s']
        open = data['o']
        high = data['h']
        low = data['l']
        close = data['c']
        volume = data['v']
        trade = data['n']
        tev = TickEvent(instrument, timestamp, open, high, low, close, volume, trade)
        self.events_queue.put(tev)

There is also a similar question related to this issue in this link but i am not able to figure out how to resolve this issue with a queue variable.

Event.py:

class Event(object):
    pass


class TickEvent(Event):
    def __init__(self, instrument, time, open, high, low, close, volume, trade):
        self.type = 'TICK'
        self.instrument = instrument
        self.time = time
        self.open = open
        self.high = high
        self.low = low
        self.close = close
        self.high = high
        self.volume = volume
        self.trade = trade
        # print(self.type, self.instrument, self.open, self.close, self.high)

    def __str__(self):
        return "Type: %s, Instrument: %s, Time: %s, open: %s, high: %s, low: %s, close: %s, volume: %s, trade: %s" % (
            str(self.type), str(self.instrument),
            str(self.time), str(self.open), str(self.high),
            str(self.low), str(self.close), str(self.volume),
            str(self.trade)
        )

    def __repr__(self):
        return str(self)


from Calling queue variable processed outside of __init__ getting Empty values in python

No comments:

Post a Comment