0

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)
27
  • Where are you accessing prices.events_queue? Commented Jul 25, 2021 at 7:34
  • I am accessing with fetch_data function with data_q variable in fetch_thread Commented Jul 25, 2021 at 8:22
  • 1
    Why don't you use the block=True option when calling get()? Otherwise you'll get an error if the prices thread doesn't queue something fast enough. Commented Jul 25, 2021 at 8:30
  • This might be working but data = data_q.get(False) print(data) is not printing any data in my console, how should i make sure that i am receiving the data? Commented Jul 25, 2021 at 9:18
  • are you sure it's putting into the queue? Add a print statement to the prices thread. Commented Jul 25, 2021 at 9:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.