Search code examples
pythonoopwebsocketqueuepython-class

Unable to get Queue value with Python Class Structure / Transform functions to class structure


I have written an Object-Oriented script in Python to retrieve and process live Binance tick data. The class stores the tick data in a Queue. For further processing, the Queue data is loaded in get_queue() method in the same class. Here is my code:

import websocket, json
from datetime import datetime
import threading
import time
import queue

class stream:

    def __init__(self, event_queue):
        self.event_queue = event_queue

    def on_message(self, ws, message):
        data = json.loads(message)
        timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')
        symbol = data['s']
        open = data['o']
        high = data['h']
        low = data['l']
        close = data['c']
        volume = data['v']
        trade = data['n'] #No. of Trades
        tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt: {trade}'
        self.event_queue.put(tick)

    def on_close(self, ws, message):
        print("bang")
    
    def run(self):
        self.socket = websocket.WebSocketApp(
            "wss://stream.binance.com:9443/ws/btcusdt@ticker/ethbtc@ticker/bnbbtc@ticker/wavesbtc@ticker/stratbtc@ticker/ethup@ticker/yfiup@ticker/xrpup@ticker",
            on_message=self.on_message,
            on_close=self.on_close)
        self.wst = threading.Thread(target=lambda: self.socket.run_forever())
        self.wst.daemon = True
        self.wst.start()
        while not self.socket.sock.connected: #and conn_timeout:
            print("this")
            time.sleep(1)
        while self.socket.sock is not None:
            print("that")
            time.sleep(10)

    def get_queue(self):
        while 1:
            print("This is this: ", self.event_queue.get(True))
        

if __name__ == "__main__":
    message_queue = queue.Queue()
    stream = stream(event_queue=message_queue)
    thread = threading.Thread(target=stream.get_queue,daemon=True)
    thread.start()
    stream.run()
    

Using the Object-Oriented approach (given above), my Queue ends up giving no values to my terminal. However, with the procedural approach (give below) I get my desired results. Here is the procedural implementation:

import websocket, json
from datetime import datetime
import threading
import time
import queue

events = queue.Queue()
socket = f'wss://stream.binance.com:9443/ws/btcusdt@ticker/ethbtc@ticker/bnbbtc@ticker/wavesbtc@ticker/stratbtc@ticker/ethup@ticker/yfiup@ticker/xrpup@ticker'

def on_message(ws, message):
    data = json.loads(message)
    timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')
    symbol = data['s']
    open = data['o']
    high = data['h']
    low = data['l']
    close = data['c']
    volume = data['v']
    trade = data['n'] #No. of Trades
    tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt: {trade}'
    events.put(tick)
    # print("This is this: ", events.get(True))

def on_close(ws, message):
    print("bang")

def get_queue():
    while 1:
        print(events.get(True))

def run():
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp(
        socket, on_message=on_message, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()
    while not ws.sock.connected: #and conn_timeout:
        print("this")
        time.sleep(1)
    while ws.sock is not None:
        print("that")
        time.sleep(10)
    
def main():
    get_queue_th = threading.Thread(target=get_queue)
    get_queue_th.daemon = True
    get_queue_th.start()
    run()
    



if __name__ == "__main__":
    main()

I want to take the Object-Oriented approach, but I can not spot the bug. Any help would be greatly appreciated.

Thanks!


Solution

  • I just capitalize class name and kept rest of the things as it is. It seems working for me.

    Code:

    import websocket
    import json
    import threading
    import time
    import queue
    from datetime import datetime
    
    class Stream:
    
        def __init__(self, event_queue):
            self.event_queue = event_queue
    
        def on_message(self, ws, message):
            data = json.loads(message)
            timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')
            symbol = data['s']
            open = data['o']
            high = data['h']
            low = data['l']
            close = data['c']
            volume = data['v']
            trade = data['n'] #No. of Trades
            tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt: {trade}'
            self.event_queue.put(tick)
    
        def on_close(self, ws, message):
            print("bang")
        
        def run(self):
            self.socket = websocket.WebSocketApp(
                "wss://stream.binance.com:9443/ws/btcusdt@ticker/ethbtc@ticker/bnbbtc@ticker/wavesbtc@ticker/stratbtc@ticker/ethup@ticker/yfiup@ticker/xrpup@ticker",
                on_message=self.on_message,
                on_close=self.on_close)
            self.wst = threading.Thread(target=lambda: self.socket.run_forever())
            self.wst.daemon = True
            self.wst.start()
            while not self.socket.sock.connected: #and conn_timeout:
                print("this")
                time.sleep(1)
            while self.socket.sock is not None:
                print("that")
                time.sleep(10)
    
        def get_queue(self):
            while 1:
                print("This is this: ", self.event_queue.get(True))
            
    
    if __name__ == "__main__":
        message_queue = queue.Queue()
        stream = Stream(event_queue=message_queue)
        thread = threading.Thread(target=stream.get_queue,daemon=True)
        thread.start()
        stream.run()
    

    Terminal Output:

    this
    This is this:  tick :timestamp: 2021-08-18 04:33:09 :symbol: BNBBTC :close_price: 0.00885000 :volume: 218003.86000000:open_price: 0.00922500:high_price: 0.00927300:low_price: 0.00870100:trade_qyt: 134006
    that
    This is this:  tick :timestamp: 2021-08-18 04:33:09 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.83900000:open_price: 0.06892500:high_price: 0.06994900:low_price: 0.06655500:trade_qyt: 252554
    This is this:  tick :timestamp: 2021-08-18 04:33:09 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.09000000:open_price: 0.00052000:high_price: 0.00052770:low_price: 0.00047690:trade_qyt: 16067
    This is this:  tick :timestamp: 2021-08-18 04:33:09 :symbol: BTCUSDT :close_price: 45032.73000000 :volume: 61188.11215700:open_price: 46346.01000000:high_price: 47160.00000000:low_price: 44203.28000000:trade_qyt: 1831000
    This is this:  tick :timestamp: 2021-08-18 04:33:10 :symbol: BNBBTC :close_price: 0.00885000 :volume: 218003.86000000:open_price: 0.00922500:high_price: 0.00927300:low_price: 0.00870100:trade_qyt: 134006
    This is this:  tick :timestamp: 2021-08-18 04:33:10 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.83900000:open_price: 0.06892500:high_price: 0.06994900:low_price: 0.06655500:trade_qyt: 252554
    This is this:  tick :timestamp: 2021-08-18 04:33:10 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.40000000:open_price: 0.00052000:high_price: 0.00052770:low_price: 0.00047690:trade_qyt: 16068
    This is this:  tick :timestamp: 2021-08-18 04:33:10 :symbol: BTCUSDT :close_price: 45035.31000000 :volume: 61188.04606300:open_price: 46346.01000000:high_price: 47160.00000000:low_price: 44203.28000000:trade_qyt: 1830996
    This is this:  tick :timestamp: 2021-08-18 04:33:11 :symbol: BNBBTC :close_price: 0.00885000 :volume: 218003.86000000:open_price: 0.00922500:high_price: 0.00927300:low_price: 0.00870100:trade_qyt: 134006
    This is this:  tick :timestamp: 2021-08-18 04:33:11 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.80700000:open_price: 0.06891900:high_price: 0.06994900:low_price: 0.06655500:trade_qyt: 252553
    This is this:  tick :timestamp: 2021-08-18 04:33:11 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.64000000:open_price: 0.00052000:high_price: 0.00052770:low_price: 0.00047690:trade_qyt: 16069
    This is this:  tick :timestamp: 2021-08-18 04:33:11 :symbol: BTCUSDT :close_price: 45040.84000000 :volume: 61187.84066900:open_price: 46346.01000000:high_price: 47160.00000000:low_price: 44203.28000000:trade_qyt: 1830984
    This is this:  tick :timestamp: 2021-08-18 04:33:12 :symbol: BNBBTC :close_price: 0.00885300 :volume: 218005.44000000:open_price: 0.00922500:high_price: 0.00927300:low_price: 0.00870100:trade_qyt: 134007
    This is this:  tick :timestamp: 2021-08-18 04:33:12 :symbol: ETHBTC :close_price: 0.06768000 :volume: 149723.81200000:open_price: 0.06891900:high_price: 0.06994900:low_price: 0.06655500:trade_qyt: 252554
    This is this:  tick :timestamp: 2021-08-18 04:33:12 :symbol: BTCUSDT :close_price: 45040.83000000 :volume: 61187.58821700:open_price: 46346.00000000:high_price: 47160.00000000:low_price: 44203.28000000:trade_qyt: 1830959
    This is this:  tick :timestamp: 2021-08-18 04:33:13 :symbol: BNBBTC :close_price: 0.00885300 :volume: 218000.84000000:open_price: 0.00922500:high_price: 0.00927300:low_price: 0.00870100:trade_qyt: 134005
    This is this:  tick :timestamp: 2021-08-18 04:33:13 :symbol: ETHBTC :close_price: 0.06767700 :volume: 149724.88400000:open_price: 0.06891900:high_price: 0.06994900:low_price: 0.06655500:trade_qyt: 252553
    This is this:  tick :timestamp: 2021-08-18 04:33:13 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.64000000:open_price: 0.00052000:high_price: 0.00052770:low_price: 0.00047690:trade_qyt: 16069
    

    You have to install 2 python package to run this script.

    1. pip install websocket
    2. pip install websocket-client

    If you have existing one then please make sure that you have latest one