美文网首页
python守护进程实现

python守护进程实现

作者: 明明就_c565 | 来源:发表于2020-05-09 17:45 被阅读0次

一个非常好用的python库

安装

pip install daemonocle

简单使用

import sys

import time

import daemonocle

#This is your daemon. It sleeps, and then sleeps again.

def main():

    while True: 

     time.sleep(10)

if __name__ == '__main__': 

     daemon = daemonocle.Daemon(worker=main, pidfile='/var/run/daemonocle_example.pid') 

     daemon.do_action(sys.argv[1]) # start|stop|restart

高级使用

测试文件crm.py

def main_task():

    while True:

        while task_queue.empty() is not True:

            data = task_queue.get()

            task = loop.create_task(pub_conf(data))

            loop.run_until_complete(task)

        time.sleep(0.1)

# test

@app.route('/', methods=['POST','GET','PUT','DELETE'])

def show_post():

    data = json.loads(request.get_data().decode('utf-8').lower())

    #print(data)

    return {'rcode': 0, 'description': 'Success'}

@click.command(cls=DaemonCLI, daemon_params={'pidfile': '/home/heweiwei/test/crm_example.pid'})

def main():

    threading._start_new_thread(main_task,())

    app.run(host=crm_cfg['net']['ip'],port=int(crm_cfg['net']['port']),debug=False)

if __name__ == '__main__':

    main()

使用举例

[heweiwei@localhost src]$ python crm.py

Usage: crm.py [<options>] <command> [<args>]...

Options:

  --help  Show this message and exit.

Commands:

  start    Start the daemon.

  stop    Stop the daemon.

  restart  Stop then start the daemon.

  status  Get the status of the daemon.

[heweiwei@localhost src]$

[heweiwei@localhost src]$

[heweiwei@localhost src]$ python crm.py start --help

Usage: crm.py start [<options>]

  Start the daemon.

Options:

  --debug  Do NOT detach and run in the background.

  --help  Show this message and exit.

[heweiwei@localhost src]$

相关文章

  • Python实现守护进程

    概念 守护进程(Daemon)也称为精灵进程是一种生存期较长的一种进程。它们独立于控制终端并且周期性的执行某种任务...

  • python守护进程实现

    一个非常好用的python库 安装 pip install daemonocle 简单使用 import sysi...

  • python库介绍-python-daemon: 实现pytho

    简介 python-daemon实现Unix守护进程。 参考:PEP 3143 该库实现了PEP 3143“标准守...

  • 4.5、守护进程及信号处理实战

    1、守护进程功能的实现守护进程融入项目,解放终端。相关配置:Daemon = 1;按照守护进程的方式运行守护进程如...

  • Python daemon守护进程!

    Python daemon 守护进程学习与总结。 守护进程 定义 守护进程是生存期长的一种进程。它们独立于控制终端...

  • python管理windows系统tomcat服务

    基于python实现的功能更强大的tomcat 守护进程。目前实现的功能: 1、通过性更强 ---通过修改conf...

  • 进程守护进程

    什么是守护进程?Linux 的大多数服务器就是用守护进程实现的,使用ps -axj可以查看守护进程: 守护进程基本...

  • 进程守护

    windows服务守护进程bat脚本、windows窗体守护进程bat脚本 windows 之 bat 实现进程守...

  • linux spervisor监控进程,设置守护

    Supervisord是用Python实现的一款的进程管理工具,supervisord要求管理的程序是非守护程序,...

  • 实现守护进程

    在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性...

网友评论

      本文标题:python守护进程实现

      本文链接:https://www.haomeiwen.com/subject/toflnhtx.html