栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

要求说明:

Daemon方案:

Mqtt Client的Singleton单点模式

参考:


要求说明:
  • 终端设备(如树莓派)上实现数据采集、上报功能,作为守护程序,命令行支持 start /stop ting /restart
  • 支持实时的日志记录
  • 多线程:分为App应用与Device设备线程,分别上报应用与设备数据
  • python3 环境

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 系统服务方式

编辑 te001.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/te001.service /lib/systemd/system/
systemctl daemon-reload
sudo systemctl enable te001.service

 

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

未完待续... 

参考:

- josephernest/daemon.py

- python-daemon 

- Python: thread-safe implementation of a singleton

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/851835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号