客户端(Python)数据/消息上报守护程序实践

目录

1. 设计说明:

2. 守护进程 Daemon方案:

3. Mqtt Client的Singleton单点模式

4. 调用方法

参考:


1. 设计说明:

  • 终端设备(如树莓派)上实现数据采集、上报功能,作为守护程序,命令行支持 start /stop ting /restart
  • 支持实时的日志记录
  • 多线程支持, 分为:
    - AppCommander 应用指挥官线程,负责接受命令,指挥动作
    - EnvReporter 环境记者线程,负责收集并上报环境数据
  • python3 环境

2. 守护进程 Daemon方案:

1. daemon-python, 比较通用的方式,但不支持 start/stop/restart,不灵活

#!/usr/bin/env python

from common.models.App import *
import daemon
import common.config as Config
from common.utils.Logger import Logger

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    app = App()

    # # 获取预留文件句柄给python daemon, 否则log会在daemon时被关闭
    with daemon.DaemonContext(files_preserve=Logger.getPreserveFds()):
        app.run()

2.  Daemon.py (From "A simple unix/linux daemon in Python" by Sander Marechal )

能符合要求,choose it

#!/usr/bin/env python
import sys
import time
from common.models.App import *

if __name__ == '__main__':
    daemon = App()
    if len(sys.argv) > 1:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
    else:
        daemon.direct_run()

3. Linux 系统服务方式

编辑 te001c.service

[Unit]
Description=Te001 ClientApp Service
#After=multi-user.target
After=network.target

[Service]
Type=idle
ExecStart=/usr/bin/python /data0/Projects/te001_client/main.py
Restart = on-failure
RestartSec=5
TimeoutStartSec=infinity

[Install]
WantedBy=multi-user.target

步骤:
cp /data0/Projects/te001_client/te001c.service /lib/systemd/system/
systemctl daemon-reload
sudo systemctl enable te001c.service

3. Mqtt Client的Singleton单点模式

class MqttClient(object):
    _instance = None
    _lock = threading.Lock()
    host = user = password = ''

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            with cls._lock:
                # another thread could have created the instance
                # before we acquired the lock. So check that the
                # instance is still nonexistent.
                if not cls._instance:
                    cls._instance = super().__new__(cls, *args, **kwargs)
                    cls._instance.host = Config.MQTT_HOST
                    cls._instance.port = Config.MQTT_PORT
                    cls._instance.user = Config.MQTT_USER
                    cls._instance.password = Config.MQTT_PSW
                    cls._instance.client = mqtt.Client()
                    cls._instance.will_set(Config.TOPIC_DEVICE_STATUS, cls._instance.getLastWill())
                    cls._instance.connect()
        return cls._instance

4. 调用方法

./main.py直接启动 无论pid有否)
./main.py start守护模式启动
./main.py stop停止
./main.py restart重启

未完待续... 

参考:

josephernest/daemon.py

- python-daemon 

Python: thread-safe implementation of a singleton

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bennybi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值