如何将python paho mqtt与作业队列集成?

如何将python paho mqtt与作业队列集成?,python,paho,Python,Paho,我正在为IoT项目编写应用程序,该项目需要使用Python并订阅一些主题。收到消息后,我需要向队列中添加新作业以及相应的优先级,然后根据该优先级执行。问题是有时在同一时间可能有很多消息,我需要对它们进行优先级排序,并在前一个消息完成后执行它们 问题是我不能将两者结合起来。我正在使用的队列示例 import Queue class Job(object): def __init__(self, priority, description): self.priority =

我正在为IoT项目编写应用程序,该项目需要使用Python并订阅一些主题。收到消息后,我需要向队列中添加新作业以及相应的优先级,然后根据该优先级执行。问题是有时在同一时间可能有很多消息,我需要对它们进行优先级排序,并在前一个消息完成后执行它们

问题是我不能将两者结合起来。我正在使用的队列示例

import Queue

class Job(object):
    def __init__(self, priority, description):
        self.priority = priority
        self.description = description
        print 'New job:', description
        return
    def __cmp__(self, other):
        return cmp(self.priority, other.priority)

q = Queue.PriorityQueue()

q.put( Job(3, 'Mid-level job') )
q.put( Job(10, 'Low-level job') )
q.put( Job(1, 'Important job') )

while not q.empty():
    next_job = q.get()
    print 'Processing job:', next_job.description
问题是把底部放在哪里

while not q.empty():
        next_job = q.get()
        print 'Processing job:', next_job.description
内MQTT-paho结构

我有这个

import paho.mqtt.client as mqtt
import datetime
import json
from time import sleep

import Queue

class Job(object):
    def __init__(self, priority, description):
        self.priority = priority
        self.description = description
        print 'New job:', description
        return
    def __cmp__(self, other):
        return cmp(self.priority, other.priority)

q = Queue.PriorityQueue()

from pprint import pprint

def on_connect(client, userdata, flags, rc): 
    client.subscribe("mytopic")

def on_message(client, userdata, msg):
    #here I had the job to queqe for example
    q.put( Job(1, 'Important job') )

#where should I call the queue

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("host", 1883, 60)

client.loop_forever()
我尝试将其添加到on_消息中,但出现此错误

File "myfile.py", line 136, in <module>
    client.loop_forever()
文件“myfile.py”,第136行,在
client.loop_forever()

尝试使用:
client.loop\u start()
而不是
client.loop\u forever()


client.loop\u forever()
有时会阻止程序的执行。

尝试使用另一个线程来处理
client.loop\u forever()