Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:同时从两个WebSocket获取数据并对其进行处理_Python 3.x_Multithreading_Websocket_Multiprocessing - Fatal编程技术网

Python 3.x Python:同时从两个WebSocket获取数据并对其进行处理

Python 3.x Python:同时从两个WebSocket获取数据并对其进行处理,python-3.x,multithreading,websocket,multiprocessing,Python 3.x,Multithreading,Websocket,Multiprocessing,我正在尝试从两个不同的交易交易所API获取两个数据流,一个来自现货交易交易所,另一个来自期货交易所,以实时获取数据并进行一些处理 我可以单独连接到交换机,但我可以同时从它们获取数据 两个套接字都是使用不同的线程打开的,但我没有得到任何结果 我所尝试的: import websocket import json import pprint import threading SOCKET_SPOT = 'wss://stream.binance.com:9443/ws/btcusdt@trade

我正在尝试从两个不同的交易交易所API获取两个数据流,一个来自现货交易交易所,另一个来自期货交易所,以实时获取数据并进行一些处理

我可以单独连接到交换机,但我可以同时从它们获取数据

两个套接字都是使用不同的线程打开的,但我没有得到任何结果

我所尝试的:

import websocket
import json 
import pprint
import threading

SOCKET_SPOT = 'wss://stream.binance.com:9443/ws/btcusdt@trade'
SOCKET_FUTURES =  'wss://fstream.binance.com/ws/btcusdt@trade'


class Client(threading.Thread):
    def __init__(self, url, exchange):
        super().__init__()
        # create websocket connection
        self.ws = websocket.WebSocketApp(
            url=url,
            on_message=self.on_message,
            on_error=self.on_error,
            on_close=self.on_close,
            on_open=self.on_open
        )

        # exchange name
        self.exchange = exchange

    # keep connection alive
    def run(self):
        while True:
            self.ws.run_forever()

    # convert message to dict, process update
    def on_message(self, message):
        data = json.loads(message)
        pprint.pprint(data)
        
    # catch errors
    def on_error(self, error):
        print(error)

    # run when websocket is closed
    def on_close(self):
        print("### closed ###")

    # run when websocket is initialised
    def on_open(self):
        print(f'Connected to {self.exchange}\n')



bs = Client(SOCKET_SPOT, "Binance Spot")
bf = Client(SOCKET_FUTURES, "Binance futures")

bs.start()
bf.start()

bs.join()
bf.join()
你能帮忙吗

单独工作代码:

import websocket
import json 
import pprint

#SOCKET_SPOT = 'wss://stream.binance.com:9443/ws/btcusdt@trade'
SOCKET_FUTURES =  'wss://fstream.binance.com/ws/btcusdt@trade'


def on_open(ws):
    print("open connection")

def on_close(ws):
    print("connection closed")

def on_message(ws, message):
    print("Message received")
    json_message = json.loads(message)
    #print(pd.DataFrame(json_message))
    pprint.pprint(json_message)


ws = websocket.WebSocketApp(SOCKET_FUTURES, on_open=on_open, on_close=on_close , on_message=on_message)
ws.run_forever()
您是否在日志中看到“连接到…”和正确的url?