我想用一个额外的 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:nt {}'.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()
我可以让它像这样修改我的 wsgi.py:
import os
import time
import threading
import logging
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "iot.settings")
application = get_wsgi_application()
logger = logging.getLogger('mqtt')
from iot.settings import MQTT_CONNECTION, MQTT_BROKER
if MQTT_CONNECTION:
logger.debug('Connecting to Broker...')
from utils_mqtt_client.mqtt_client import run_client
run_client()
else:
logger.debug('Starting without MQTT connection')