一个非常好用的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]$
网友评论