猿问

Django和Apache:为MQTT Client启动附加线程

我想用一个额外的mqtt-client启动一个apache-server。我在生产中使用Django和Apache。要启动我的mqqt客户端,我编辑了wsgi.py它,如果我使用的是开发服务器(python3 manage.py runserver),则可以正常运行。但是它不能在生产中工作。请问有什么帮助或有想法如何让apache在生产中起作用吗?


wsgi.py:


import os

import threading

import logging


logger = logging.getLogger('mqtt')


from django.core.wsgi import get_wsgi_application


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "iot.settings")



from iot.settings import MQTT_CONNECTION, MQTT_BROKER

if MQTT_CONNECTION:

    logger.debug('Connecting to Broker...')

    try:

        from utils_mqtt_client.mqtt_client import run_client

        run_client()

    except Exception as e:

        logger.debug('Could not conntect to MQTT Broker:\n\t {}'.format(e))

else:

    logger.debug('Starting without MQTT connection')



application = get_wsgi_application()

mqqt客户端:


import json

import threading

import logging

import paho.mqtt.client as mqtt


from device.models import Device

from value.models import Value


from iot.settings import MQTT_BROKER


logger = logging.getLogger('mqtt')


topics = ['config']

CLIENT = mqtt.Client('webserver')


# [...]


def run_client():

    devices = Device.objects.all()


    CLIENT.on_connect = on_connect

    CLIENT.on_message = on_message


    for device in devices:

        subscribe(CLIENT, device)


    CLIENT.connect(MQTT_BROKER)


    CLIENT.loop_start()


动漫人物
浏览 188回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答