1
我有一個WebSocket客戶端,我想處理線程中的消息,這些線程根據數據的內容分割爲多個隊列。如何將Python線程放入多個隊列並按照添加的順序運行它?
我按照特定的順序接收消息,我想運行按此順序處理消息的函數,但只能在類別中執行。
我設法做的是創建一個不尊重的消息訂單的解決方案:
def start():
ws = websocket.WebSocketApp(
"ws://127.0.0.1:8080/",
on_message=on_message,
)
Thread(target=ws.run_forever).start()
def on_message(ws, data):
Thread(target=process_data, args=(data,).start() # here I want to preserve order
# here should be categorizing and queuing threads
def process_data(data):
# here should wait for running thread in its category to end,
# and then become a running thread and process data
我不能使用的Thread.join()在ON_MESSAGE,因爲它會阻塞主線程,我想用這種方法保存消息順序。它需要在分類後進行阻塞,每個類別都在自己的線程隊列中。
也許我應該排隊消息,而不是線程?