Я пишу приложение для проекта 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_message, но я получаю эту ошибку
File "myfile.py", line 136, in <module>
client.loop_forever()