问题描述
我用 Python
编写了一个面向对象的脚本来检索和处理实时 Binance 报价数据.该类将刻度数据存储在 Queue
中.为了进一步处理,Queue
数据加载在同一个类中的 get_queue() 方法
中.这是我的代码:
导入websocket、json从日期时间导入日期时间进口螺纹导入时间导入队列类流:def __init__(self, event_queue):self.event_queue = event_queuedef on_message(self, ws, message):数据 = json.loads(消息)时间戳 = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')符号 = 数据['s']打开 = 数据 ['o']高 = 数据 ['h']低 = 数据['l']关闭 = 数据 ['c']音量 = 数据['v']贸易=数据['n'] #没有.行业tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt:{贸易}'self.event_queue.put(tick)def on_close(self, ws, message):打印(砰")定义运行(自我):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 = 真self.wst.start()而不是 self.socket.sock.connected: #and conn_timeout:打印(这个")时间.睡眠(1)而 self.socket.sock 不是 None:打印(那个")时间.睡眠(10)def get_queue(self):而 1:打印(这是这个:",self.event_queue.get(True))如果 __name__ == __main__":message_queue = queue.Queue()流 = 流(事件队列 = 消息队列)线程 = threading.Thread(target=stream.get_queue,daemon=True)线程开始()流运行()
使用面向对象的方法(上面给出),我的队列最终没有给我的终端提供任何值.但是,通过程序方法(在下面给出),我得到了我想要的结果.这是程序实现:
导入websocket、json从日期时间导入日期时间进口螺纹导入时间导入队列事件 = 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):数据 = json.loads(消息)时间戳 = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')符号 = 数据['s']打开 = 数据 ['o']高 = 数据 ['h']低 = 数据['l']关闭 = 数据 ['c']音量 = 数据['v']贸易=数据['n'] #没有.行业tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt:{贸易}'事件.put(tick)# print("这是这个:", events.get(True))def on_close(ws, message):打印(砰")def get_queue():而 1:打印(事件.get(真))定义运行():websocket.enableTrace(假)ws = websocket.WebSocketApp(套接字,on_message=on_message,on_close=on_close)wst = threading.Thread(target=ws.run_forever)wst.daemon = 真wst.start()而不是 ws.sock.connected: #and conn_timeout:打印(这个")时间.睡眠(1)而 ws.sock 不是 None:打印(那个")时间.睡眠(10)定义主():get_queue_th = threading.Thread(target=get_queue)get_queue_th.daemon = Trueget_queue_th.start()跑()如果 __name__ == __main__":主要的()
我想采用面向对象的方法,但我无法发现错误.任何帮助将不胜感激.
谢谢!
我只是将类名大写,其余部分保持原样.它似乎对我有用.
代码:
导入websocket导入json进口螺纹导入时间导入队列从日期时间导入日期时间类流:def __init__(self, event_queue):self.event_queue = event_queuedef on_message(self, ws, message):数据 = json.loads(消息)时间戳 = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%S')符号 = 数据['s']打开 = 数据 ['o']高 = 数据 ['h']低 = 数据 ['l']关闭 = 数据 ['c']音量 = 数据['v']贸易=数据['n'] #没有.行业tick = f'tick :timestamp: {timestamp} :symbol: {symbol} :close_price: {close} :volume: {volume}:open_price: {open}:high_price: {high}:low_price: {low}:trade_qyt:{贸易}'self.event_queue.put(tick)def on_close(self, ws, message):打印(砰")定义运行(自我):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 = 真self.wst.start()而不是 self.socket.sock.connected: #and conn_timeout:打印(这个")时间.睡眠(1)而 self.socket.sock 不是 None:打印(那个")时间.睡眠(10)def get_queue(self):而 1:打印(这是这个:",self.event_queue.get(True))如果 __name__ == __main__":message_queue = queue.Queue()流 = 流(事件队列 = 消息队列)线程 = threading.Thread(target=stream.get_queue,daemon=True)线程开始()流运行()
终端输出:
这个这是这样的:蜱:时间戳:2021年8月18日4时33分09秒:符号:BNBBTC:close_price:0.00885000:体积:218003.86000000:open_price:0.00922500:HIGH_PRICE:0.00927300:LOW_PRICE:0.00870100:trade_qyt:134006那这是这样的:tick :timestamp: 2021-08-18 04:33:09 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.83900000:open_price: 0.06892500:0505205000:high_505q505q5200000000000000000000000004这是这样的:tick :timestamp: 2021-08-18 04:33:09 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.09000000:open_price: 0.00052000:7006price_07trade_0700000000000000000000007000000000070000000000000000000006这是这样的:蜱:时间戳:2021年8月18日4时33分09秒:符号:BTCUSDT:close_price:45032.73000000:体积:61188.11215700:open_price:46346.01000000:HIGH_PRICE:47160.00000000:LOW_PRICE:44203.28000000:trade_qyt:1831000这是这样的:蜱:时间戳:2021年8月18日4时33分十秒:符号:BNBBTC:close_price:0.00885000:体积:218003.86000000:open_price:0.00922500:HIGH_PRICE:0.00927300:LOW_PRICE:0.00870100:trade_qyt:134006这是这样的:tick :timestamp: 2021-08-18 04:33:10 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.83900000:open_price: 0.06892500:0652050000:high_5205q520000052000000000000000004这是这样的:tick :timestamp: 2021-08-18 04:33:10 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.40000000:open_price: 0.00052000:070000000000000000000000700000000000000000000000000000000000000000000000000000000000000000000000000000!这是这样的:蜱:时间戳:2021年8月18日四点33分十秒:符号:BTCUSDT:close_price:45035.31000000:体积:61188.04606300:open_price:46346.01000000:HIGH_PRICE:47160.00000000:LOW_PRICE:44203.28000000:trade_qyt:1830996这是这样的:蜱:时间戳:2021年8月18日4时33分11秒:符号:BNBBTC:close_price:0.00885000:体积:218003.86000000:open_price:0.00922500:HIGH_PRICE:0.00927300:LOW_PRICE:0.00870100:trade_qyt:134006这是这样的:tick :timestamp: 2021-08-18 04:33:11 :symbol: ETHBTC :close_price: 0.06768900 :volume: 149723.80700000:open_price: 0.06891900:05520000:high_505205q5q300000520000000000004这是这样的:tick :timestamp: 2021-08-18 04:33:11 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.64000000:open_price: 0.00052000:070000000.7000000000007000000000700000000000000000000000000000000000000000000000707070600000000000000000000000000700707060700007000000007000700700600700700600707007涌}这是这样的:蜱:时间戳:2021年8月18日4时33分11秒:符号:BTCUSDT:close_price:45040.84000000:体积:61187.84066900:open_price:46346.01000000:HIGH_PRICE:47160.00000000:LOW_PRICE:44203.28000000:trade_qyt:1830984这是这样的:tick :timestamp: 2021-08-18 04:33:12 :symbol: BNBBTC :close_price: 0.00885300 :volume: 218005.44000000:open_price: 0.00922500:000100000000000000000000000000000000000000000000000000000000000000000007070070007000700700000000007这是这样的:tick :timestamp: 2021-08-18 04:33:12 :symbol: ETHBTC :close_price: 0.06768000 :volume: 149723.81200000:open_price: 0.06891900:05520000:high_5205q520000052000000000000000004这是这样的:蜱:时间戳:2021年8月18日4时33分十二秒:符号:BTCUSDT:close_price:45040.83000000:体积:61187.58821700:open_price:46346.00000000:HIGH_PRICE:47160.00000000:LOW_PRICE:44203.28000000:trade_qyt:1830959这是这样的:蜱:时间戳:2021年8月18日四时33分13秒:符号:BNBBTC:close_price:0.00885300:体积:218000.84000000:open_price:0.00922500:HIGH_PRICE:0.00927300:LOW_PRICE:0.00870100:trade_qyt:134005这是这样的:蜱:时间戳:2021年8月18日4时33分13秒:符号:ETHBTC:close_price:0.06767700:体积:149724.88400000:open_price:0.06891900:HIGH_PRICE:0.06994900:LOW_PRICE:0.06655500:trade_qyt:252553这是这样的:tick :timestamp: 2021-08-18 04:33:13 :symbol: WAVESBTC :close_price: 0.00049340 :volume: 201920.64000000:open_price: 0.00052000:070000000.7000000000070000000000000000000000000000000000000000000000007号07060000000000000000000000000000000000000000:01:00:00:00:00049340 :volume: 201920.64000000:open_price:
您必须安装 2 个 python 包才能运行此脚本.
pip install websocket
pip install websocket-client
如果您有现有的,请确保您有最新的
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!
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.
pip install websocket
pip install websocket-client
这篇关于无法使用 Python 类结构获取队列值/将函数转换为类结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!