MQTT消息未从Celery Beat Task中发布

时间:2019-04-26 09:21:19

标签: python-3.x celery mqtt paho

我希望定期将传感器数据定期发布到Mosquitto服务器。我正在使用Celery Beat调度程序来这样做。芹菜工人正在使用该任务,但该消息未发布到代理。

代码:

from flask import Flask
from celery import Celery
import paho.mqtt.client as mqtt

app = Flask(__name__)
app.debug = True

celery = Celery(app.name, broker= 'redis://localhost:6379/0')

app.config['CELERYBEAT_SCHEDULE']  = {
    'update_temp': {
        'task': 'app.update_temperature',
        'schedule': 10.0,        
    }
}
app.config['CELERY_IMPORTS'] = ('app')

celery.conf.update(app.config)

@celery.task
def update_temperature():
    client.publish("temperature/sensor01", payload = "temperature_data")

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("temperature/sensor01")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

def on_publish(client, userdata, mid):
    print('Message Published')

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.connect("localhost", 1883, 60)
client.loop_start()

@app.route('/')
def view():
    return "Hello, Flask is up and running!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

芹菜工人日志:

[2019-04-26 14:48:11,955: INFO/MainProcess] Received task: app.update_temperature[53363e79-3dff-4275-81d7-f15007c69d8f]  
[2019-04-26 14:48:11,964: INFO/ForkPoolWorker-4] Task app.update_temperature[53363e79-3dff-4275-81d7-f15007c69d8f] succeeded in 0.0023796100285835564s: None

0 个答案:

没有答案