Baisse des performances Websocket avec des threads supplémentaires
Je suis nouveau sur les websockets / threading et j'aimerais de l'aide pour déboguer un problème de performances que je rencontre avec les websockets. Voir mon code ci-dessous - si j'inclus les threads supplémentaires - mes performances websocket seront affectées. Si je supprime les threads, les performances du websocket seront vraiment bonnes. Comment puis-je conserver les threads ET conserver de bonnes performances Websocket ? Dois-je ajuster la priorité de mes threads ou quelque chose... ?
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)
Solution du problème
Vous lancez un tas de threads qui ont self.wheee
pour cible, qui est unwhile True
boucle qui ne fait rien d'utile
Commentaires
Enregistrer un commentaire