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)
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Scrappy Coco
  • 96
  • 3
  • 13
  • Where are you accessing `prices.events_queue`? – Barmar Jul 25 '21 at 07:34
  • I am accessing with `fetch_data` function with `data_q` variable in `fetch_thread` – Scrappy Coco Jul 25 '21 at 08: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. – Barmar Jul 25 '21 at 08: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? – Scrappy Coco Jul 25 '21 at 09:18
  • are you sure it's putting into the queue? Add a print statement to the prices thread. – Barmar Jul 25 '21 at 09:21
  • Yes i am sure i have put `data_q.put("checking")` in 'fetch_data' function below 'time.sleep(10)' line and it is printing that string in my console but not the data received from websocket – Scrappy Coco Jul 25 '21 at 09:33
  • I mean put `print('Queuing', tev)` in the queuing function. – Barmar Jul 25 '21 at 09:34
  • To call `self.tev` first i need to call `self.on_message` function because the `self.tev` is not defined in the `class` directly. It is throwing not defined error. If i defined in `__init__` with 'None' it will get only `None` value. Please check my provided link down in the question – Scrappy Coco Jul 25 '21 at 09:46
  • I meant to put that right before `self.events_queue.put(tev)` so you know that you're queuing something. – Barmar Jul 25 '21 at 09:48
  • I understand what you meant but it does not `print` any variable that are inside that `StreamingForexPrices` to my main console. – Scrappy Coco Jul 25 '21 at 09:54
  • Then the `on_message` function is never being called, and that's why nothing is in the queue. – Barmar Jul 25 '21 at 09:55
  • Well i am calling it with `conn` function with `self.on_message`. I have checked with other git hub codes they are doing in the same way but it is not going with me. – Scrappy Coco Jul 25 '21 at 09:58
  • I can see what you're doing very clearly. For some reason you're not getting any messages from the websocket connection. Maybe try turning on tracing? – Barmar Jul 25 '21 at 10:02
  • Enabling tracing throws me call back error on websocket. Sir would you please run my code on your system to make a better understanding about the issue. – Scrappy Coco Jul 25 '21 at 10:12
  • I'm getting an error with `pip install event` because it needs python 2.x. – Barmar Jul 25 '21 at 10:23
  • I have edited the question with `Event` class Please check. – Scrappy Coco Jul 25 '21 at 10:26
  • I'm getting errors: module 'websocket' has no attribute 'WebSocketApp' – Barmar Jul 25 '21 at 10:33
  • try `pip install websocket-client` – Scrappy Coco Jul 25 '21 at 10:34
  • I added `print('in on_message')` to the `on_message` function, it never printed anything. So it's not getting anything from the socket. – Barmar Jul 25 '21 at 10:37
  • I think the problem is with the `class` structure of python unless the function is not called the variable inside that function will not be accessed as you can see in my provided link. do you have any other suggestion? – Scrappy Coco Jul 25 '21 at 10:40
  • It has nothing to do with variables. The `on_message` function is never being called. – Barmar Jul 25 '21 at 10:40
  • The `on_message` function is being called with `self.ws.run_forever()` and it is in their documentation too. you can check a similar application in github https://github.com/sc1f/perspective-order-book/blob/84de77b3810aee4651ebcdeb5e127c9c3e4e9478/src/server/server.py – Scrappy Coco Jul 25 '21 at 10:44
  • It only gets called when the websocket receives a message. – Barmar Jul 25 '21 at 10:45
  • It is a live server i have tested it without `class` structure of python and it is working well but not working properly with `class` structure. – Scrappy Coco Jul 25 '21 at 10:47
  • You can check this link to get a view of binance through websocket https://github.com/Ebanchous/binance_websockets/blob/3c4c9bec47c44c09a6432a5d5fadd29a1e33b4cf/binance_websockets.py – Scrappy Coco Jul 25 '21 at 10:50
  • I am not sure what your asking. Running this code gives me "checking11..\nchecking2...\nTYPE.\nchecking222...\n" and so on. Are you not getting this? If so, you should check if your network works (i.e. I think the problem lies outside Python code) – Karthik Sriram Aug 02 '21 at 19:45
  • Thanks for your response. In my class `StreamingForexPrices` i am putting received ticks value inside the queue with `on_message` function i.e `self.events_queue.put(tev)` and trying to get the values from the queue with `fetch_data` function in `main.py` script. This should print the received data in my terminal but it is getting `empty queue`. – Scrappy Coco Aug 03 '21 at 02:44

0 Answers0