If you start a child process directly, the child process will always exist when you exit the main process. It is recommended to set it as a daemon process
import sys
import signal
import threading
import time
from datetime import datetime
def quit(signum, frame):
sys.exit()
def process_fun():
while True:
print datetime.now()
time.sleep(1)
if __name__ == '__main__':
try:
signal.signal(signal.SIGINT, quit)
signal.signal(signal.SIGTERM, quit)
p = threading.Thread(target=process_fun)
#注册成为主进程
p.setDaemon(True)
p.start()
#如果没有主进程, 就用循环代理
while True:
pass
except Exception as e:
pass
You can consider Advanced Python Scheduler (http://apscheduler.readthedoc... It can carry out extremely complex timing design, every few seconds, minutes, or a specific moment of a certain day, etc., it can block the process, and it can be in the background , all according to your requirements
Python task scheduling module – APScheduler (click to view)
APScheduler is a Python scheduled task framework, which is very convenient to use. It provides tasks based on date, fixed time interval and crontab type, and can persist tasks and run applications in daemon mode.
The following is a simple example, printing hello world every 10 seconds
You can open another thread to do this specifically. The py2 code is as follows. If it is py3, please adjust the syntax yourself
threading.Timer
If you start a child process directly, the child process will always exist when you exit the main process. It is recommended to set it as a daemon process
You can consider Advanced Python Scheduler (http://apscheduler.readthedoc...
.It can carry out extremely complex timing design, every few seconds, minutes, or a specific moment of a certain day, etc., it can block the process, and it can be in the background , all according to your requirements
Python task scheduling module – APScheduler (click to view)
APScheduler is a Python scheduled task framework, which is very convenient to use. It provides tasks based on date, fixed time interval and crontab type, and can persist tasks and run applications in daemon mode.
The following is a simple example, printing hello world every 10 seconds
Execution result: