如何解决Python Websocket 未收到消息
我正在尝试创建一个线程 websocket 系统来从 binance 流式传输数据。我创建了一个名为“crypto”的类,它代表将基于币安流进行交易的特定货币。这是主线程。它实例化一个加密货币和一个客户端。它还维护一个共享的数据字典。这个想法是,当 websocket 获取新数据时,它会将其存储在主字典中并更新添加时间。然后,主线程将处理数据。我的目标是能够在各自的线程中创建多个加密流。我的流正在连接到币安,但我没有收到来自流的任何消息。
location /api/ {
proxy_pass http://127.0.0.1:5000/api/
}
这是 websocket 类:
#main thread
from client import Client
from crypto import Crypto
import constants as c
import threading
from datetime import datetime
import time
def run(orderbook,lock):
current_time = datetime.Now()
while True:
try:
#check for new update
if orderbooks['last_update'] != current_time:
print("I need to process new data")
with lock:
print("Main has the lock\n")
for crypto in orderbooks:
if crypto != 'last_update':
print("Processing {}.".format(crypto.symbol))
#Do stuff here
current_time = orderbook['last_update']
print("\nI am done updating data")
print("Main released the lock\n")
time.sleep(1)
#I am doing this so I can verify this function is getting called
print(datetime.Now())
except Exception:
pass
#Instantiate the crpto currencies
DOGE = Crypto(
symbol=c.DOGE_SYMBOL,Trade_quantity=c.DOGE_TradE_QUANTITY,buy_price=c.BUY_PRICE,sell_price=c.SELL_PRICE,position_status=c.POSITION_STATUS
)
lock = threading.Lock()
orderbooks = {
DOGE: {},"last_update": None,}
#instantiate the websockets for each crypto
#these connect to binance and use 1 minute klines
doge_client = Client(
orderbook=orderbooks,socket=c.DOGE_SOCKET,symbol=c.DOGE_SYMBOL,lock=lock,crypto=DOGE
)
#Start the stream
doge_client.run()
#start checking for new data to process
run(orderbooks,lock)
我认为我在客户端类中犯了一个错误,但我一直盯着这个太久没有弄清楚。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。