python-Websocket的性能随着线程的增加而降低
发布时间:2022-04-20 07:37:02 441
相关标签: # node.js
我是websockets/线程新手,希望能在调试websockets的性能问题时得到一些帮助。请参阅下面的代码——如果我包含额外的线程——我的websocket性能将受到影响。如果我删除线程,websocket的性能将非常好。如何保持线程和websocket的良好性能?我应该调整线程的优先级还是什么?
import websocket
import threading
import json
import datetime
class Trading():
# Stores the message in memory
def on_message_binance(self, ws, message):
#print(message)
self.message = message
def start(self):
self.tower = []
binance_stream = dict()
binance_stream['BTC'] = 'wss://stream.binance.com:9443/stream?streams=btcusdt@trade'
threads = dict()
self.message = ''
threads['ws'] = websocket.WebSocketApp(binance_stream['BTC'],
on_message=lambda ws, msg: self.on_message_binance(self, ws, msg))
# Launch the thread
threads['thread'] = threading.Thread(target=threads['ws'].run_forever, name='BTC')
threads['thread'].daemon = True
threads['thread'].start()
# ************************************************
# IF THESE LINES ARE INCLUDED - WEBSOCKETS WILL BE DELAYED. COMMENT OUT TO REMOVE DELAYS
for x in range(20):
threads[x] = threading.Thread(target=self.wheee, args=(self, 1), name=x)
threads[x].daemon = True
threads[x].start()
# ************************************************
prior_binmsg = ''
while True:
# Ensure that you don't continously read the same message over and over again
binmsg = self.message
if (binmsg != prior_binmsg):
prior_binmsg = binmsg
binancemsg = json.loads(binmsg)
curtime_GMT = datetime.datetime.now().timestamp()
timestamp = float(binancemsg['data']['T'])/1000
delay = curtime_GMT - timestamp
print('Websocket delay = ' + str(delay))
def wheee(self, num):
import time
while True:
wheeeeeeee = num
if __name__ == '__main__':
# Start trading!
x = Trading
x.start(x)
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报