在其他功能上使用WebSocket数据

发布于 2025-02-07 23:15:40 字数 1089 浏览 2 评论 0原文

我正在调用特定数据的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文