如何将 python paho-mqtt 与作业队列集成?
How to integrate python paho-mqtt with job queue?
我正在为需要使用 Python 的 IoT 项目编写应用程序并订阅某个主题。收到消息后,我需要将新作业添加到队列中以及相应的优先级,然后根据该优先级执行。问题是有时同一时间会有很多消息,我需要对它们进行优先级排序,并在上一个完成后执行。
问题是我无法将两者整合。我正在使用的队列示例
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_message,但出现此错误
File "myfile.py", line 136, in <module>
client.loop_forever()
尝试使用:client.loop_start()
而不是 client.loop_forever()
client.loop_forever()
有时会阻塞程序的执行。
尝试使用另一个线程来处理client.loop_forever()
。
我正在为需要使用 Python 的 IoT 项目编写应用程序并订阅某个主题。收到消息后,我需要将新作业添加到队列中以及相应的优先级,然后根据该优先级执行。问题是有时同一时间会有很多消息,我需要对它们进行优先级排序,并在上一个完成后执行。
问题是我无法将两者整合。我正在使用的队列示例
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_message,但出现此错误
File "myfile.py", line 136, in <module>
client.loop_forever()
尝试使用:client.loop_start()
而不是 client.loop_forever()
client.loop_forever()
有时会阻塞程序的执行。
尝试使用另一个线程来处理client.loop_forever()
。