【发布时间】:2020-05-21 21:14:09
【问题描述】:
我使用 Websocket 检索数据以进行进一步处理。 我不知道如何在课堂之外检索它。 我使用线程模块将 websocket 与程序的其余部分分开,这样我就可以运行一个 pyqt5 应用程序,在其中显示处理后的数据,但我无法检索它。
也许我应该使用线程以外的东西,但我不知道。 因为我可以接收大量数据并且有很多工作要做,计算、显示等。我尝试将其优化到最低限度,否则它将永远无法每秒处理我的所有请求。
import websockets
import asyncio
import json
import threading
import time
class WS(object):
def __init__(self, serveur):
self.serveur = serveur
async def connect(self):
async with websockets.connect(self.serveur) as websocket:
while True:
message = await websocket.recv()
self.data = json.loads(message)
print(self.data)
uri = "wss://www.bitmex.com/realtime?subscribe=instrument:XBTUSD"
ws = WS(uri)
loop = asyncio.get_event_loop()
th1 = threading.Thread(target=lambda: loop.run_until_complete(ws.connect()))
th1.start()
while True: # My application that will display and process the data retrieved by the websocket.
print('blabla')
time.sleep(3)
【问题讨论】:
标签: python python-3.x websocket pyqt5 python-asyncio