将 websocket 数据附加到 python 中的列表

发布于 2025-01-09 01:15:43 字数 1950 浏览 0 评论 0原文

我正在尝试将一些 websocket 数据记录到列表中并对其执行操作。列表已创建,但数据错误。我应该使用redis来存储数据并执行操作吗?我想“即时”执行操作,并在满足条件时采取行动。所以它应该尽可能快。

这是代码:

import json                                                              
import websocket 
import asyncio



this = json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': 'BTC/USD'})                      
this2 = json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': 'ETH/USD'}) 

vx = list()
def sav(ps):
  vx.append(ps)
  list2 = [x for x in vx if x != []]
  last_element = list2[-1]
  #print(last_element)
  return last_element
  
def on_open(wsapp):   
  tickers = "BTC/USD"
  wsapp.send(json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': tickers}))
  wsapp.send(this2)

def on_message(wsapp, message):                                                   
  #print(message)                                                          
  btc = list()
  eth = list()
  response = json.loads(str(message))

  if response['market'] == "BTC/USD":                    
    resp1 = response['data']['bid']
    
    btc.append(resp1)
    
    print(f'ask price btc :', resp1)
    
  else:
    resp2 = response['data']['ask']
    eth.append(resp2)
    print(f'bid price ETH/USD :', resp2)

  print(f'r btc', btc)
  print(f'r eth', eth)
  sav(btc)
  sav(eth)
  
  # I'm trying to divide btc by eth like that :
  print(sav(btc)/sav(eth))
  
  
  # print(vx)
wsapp = websocket.WebSocketApp('wss://ftx.com/ws/', on_message=on_message, on_open=on_open)
wsapp.run_forever()

脚本返回两个列表,但数据错误。当我尝试划分 btc / eth 时,什么也没有打印。我希望用最后收到的数据填充 btc/usd 和 eth 的列表,以便执行计算,但看起来函数“On_message”每次调用时都会重置列表。

ask price btc : 38125.0
r btc [38125.0]
r eth []
[38125.0]
[38125.0]
bid price ETH/USD : 2659.1
r btc []
r eth [2659.1]
[2659.1]
[2659.1]
ask price btc : 38125.0
r btc [38125.0]
r eth []
[2659.1]
[38125.0]
bid price ETH/USD : 2659.0
r btc []
r eth [2659.0]
[2659.0]
[2659.0]

I'm trying to record some websocket data to a list and perform operations on it. The list are created but with the wrong data. should I use redis to store the data and perform the operation ? I want to perform operation 'on the fly' and take actions if a condition is met. so it should be as fast as possible.

here is the code :

import json                                                              
import websocket 
import asyncio



this = json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': 'BTC/USD'})                      
this2 = json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': 'ETH/USD'}) 

vx = list()
def sav(ps):
  vx.append(ps)
  list2 = [x for x in vx if x != []]
  last_element = list2[-1]
  #print(last_element)
  return last_element
  
def on_open(wsapp):   
  tickers = "BTC/USD"
  wsapp.send(json.dumps({'op': 'subscribe', 'channel': 'ticker', 'market': tickers}))
  wsapp.send(this2)

def on_message(wsapp, message):                                                   
  #print(message)                                                          
  btc = list()
  eth = list()
  response = json.loads(str(message))

  if response['market'] == "BTC/USD":                    
    resp1 = response['data']['bid']
    
    btc.append(resp1)
    
    print(f'ask price btc :', resp1)
    
  else:
    resp2 = response['data']['ask']
    eth.append(resp2)
    print(f'bid price ETH/USD :', resp2)

  print(f'r btc', btc)
  print(f'r eth', eth)
  sav(btc)
  sav(eth)
  
  # I'm trying to divide btc by eth like that :
  print(sav(btc)/sav(eth))
  
  
  # print(vx)
wsapp = websocket.WebSocketApp('wss://ftx.com/ws/', on_message=on_message, on_open=on_open)
wsapp.run_forever()

the script return two list but the data is wrong. and nothing is printed when I try to divide btc / eth. I want the list for btc/usd and eth to be populated with the last data received in order to perform calculations but it looks like the function "On_message" reset the lists each time it's called.

ask price btc : 38125.0
r btc [38125.0]
r eth []
[38125.0]
[38125.0]
bid price ETH/USD : 2659.1
r btc []
r eth [2659.1]
[2659.1]
[2659.1]
ask price btc : 38125.0
r btc [38125.0]
r eth []
[2659.1]
[38125.0]
bid price ETH/USD : 2659.0
r btc []
r eth [2659.0]
[2659.0]
[2659.0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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