如何将Python线程放在多个队列中并按照添加的顺序运行它?

时间:2017-05-26 07:44:09

标签: python multithreading queue python-multithreading

我有一个WebSocket客户端,我希望处理根据数据内容拆分为多个队列的线程中的消息。

我按特定顺序接收消息,我想运行按此顺序处理消息的功能,但仅限于类别。

graph

我设法做的是创建一个不尊重消息顺序的解决方案:

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

我无法在on_message中使用thread.join(),因为它会阻塞主线程,我想在此方法中保留消息顺序。分类后需要阻塞,每个类别都在自己的线程队列中。

也许我应该排队消息而不是线程?

1 个答案:

答案 0 :(得分:0)

考虑这个简单的方法:

q = Queue()
def worker_thread():
    while True:
         msg = q.get()
         if msg.cat == A:
             process A
         if msg.cat == B:
             process B

Thread(target=worker_thread).start()

while True:
      # receive message
      if A:
          q.put((A, message))
      if B:
          q.put((B, message))