微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Websocket 未收到消息

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?