python-3.x 无法让Binance ThreadWebsocketManager显示价格流

enxuqcxy  于 2023-05-08  发布在  Python
关注(0)|答案(2)|浏览(233)

我目前正在尝试从Binance实时传输价格,但在运行代码后,即使程序正在运行,我也会看到一个空白屏幕。
下面是代码:

import os
from binance.client import Client
import time
from binance import ThreadedWebsocketManager
from binance import ThreadedDepthCacheManager

api_key = os.environ.get('api_key')
api_secret = os.environ.get('api_secret')
client = Client(api_key, api_secret)

def main():

    symbol = 'BTCUSDT'
    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
    
    #Start is required to initialise its internal loop
    twm.start()

    def handle_socket_message(msg):
        print(f"message type: {msg['e']}")
        print(msg)

    twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)
    
    twm.join()

if __name__ == "__main__":
   main()
qybjjes1

qybjjes11#

我最近遇到了同样的问题。
由于某种原因,我无法从ThreadedWebSocketManager获得任何响应。
在寻找答案的时候,我遇到了你的问题,经过进一步的研究,我发现它是一个known issueThreadedWebSocketManager似乎不能很好地与Python3.10一起工作。我降级到Python3.9,它工作得很好:)

2ic8powd

2ic8powd2#

from binance.streams import ThreadedWebsocketManager
from binance.streams import ThreadedDepthCacheManager

api_key = os.environ.get('api_key')
api_secret = os.environ.get('api_secret')

def main():
    symbol = 'BTCUSDT'        
    
    def handle_socket_message(msg):
        print(f"message type: {msg['e']}")
        print(msg)
    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
    twm.start()

    twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)
    twm.join()

if __name__ == "__main__":
    main()
    while True:
        pass

相关问题