在其他功能上使用WebSocket数据
我正在调用特定数据的WebSocket功能,并希望以后在其他功能上使用它。 但是,我注意到Websocket根本不会停止更新,也不会进一步检查其他功能。 我想这是由于调用ws.run_forever()。
什么是选择?你能帮助我吗?我应该为Websocket连接创建线程吗?
谢谢
import json
import websocket
import os
from math import pi
from itertools import count
from datetime import datetime, timedelta
import time
def on_message(ws,message):
json_message = json.loads(message)
cs = json_message
for coin in cs:
min_price[coin["s"]] = { "lastPrice": coin["c"], "lowPrice": coin["l"]}
with open(compare_coins, "w") as file:
json.dump(min_price, file, indent=4)
is_candle_closed = candle['x']
if is_candle_closed:
print(json.dumps(candle, indent=2))
ws = websocket.WebSocketApp(SOCK, on_open=on_open,on_close=on_close, on_message=on_message)
ws.keep_running = True
def check_below():
volatile_coins = {}
for coin in min_price:
if float(min_price[coin]["lastPrice"])>=float(min_price[coin]["lowPrice"]):
volatile_coins[coin] = min_price[coin]["lastPrice"]
return volatile_coins, len(volatile_coins), min_price
I am calling websocket functions for particular data and want to use it later on other functions.
However, I am noticing the websocket simply doesnt stop updating and doesnt go further to check the other functions.
I guess this is due to calling ws.run_forever().
What is the alternative? can you help me? Should I create a thread for the websocket connection?
Thanks
import json
import websocket
import os
from math import pi
from itertools import count
from datetime import datetime, timedelta
import time
def on_message(ws,message):
json_message = json.loads(message)
cs = json_message
for coin in cs:
min_price[coin["s"]] = { "lastPrice": coin["c"], "lowPrice": coin["l"]}
with open(compare_coins, "w") as file:
json.dump(min_price, file, indent=4)
is_candle_closed = candle['x']
if is_candle_closed:
print(json.dumps(candle, indent=2))
ws = websocket.WebSocketApp(SOCK, on_open=on_open,on_close=on_close, on_message=on_message)
ws.keep_running = True
def check_below():
volatile_coins = {}
for coin in min_price:
if float(min_price[coin]["lastPrice"])>=float(min_price[coin]["lowPrice"]):
volatile_coins[coin] = min_price[coin]["lastPrice"]
return volatile_coins, len(volatile_coins), min_price
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论