The examples in this article describe the Python daemon usage. Share to everyone for your reference. The specific analysis is as follows:
The daemon can run all the time without blocking the main program from exiting. To flag a daemon, you can set the Daemon property of the process instance to true. The code is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Import OS import time import random import sys multiprocessing import process,current_process def daemon (): p = Curre Nt_process () print "Starting id%d prccess%sn"% (p.pid,p.name) Sys.stdout.flush () time.sleep (3) print "EXITING:%SN"% p.na Me Sys.stdout.flush () def main (): P = Process (name= "Daemon", Target=daemon) p.daemon=true P.start () if __name__== "__main_" _ ": Main () Time.sleep (1) |
The daemon's print "EXITING:%SN"% p.name is not visible because the main process exits after 1 seconds.
If you want to see the print "EXITING:%SN"% p.name statement output of the daemon, you can use join, the main process waits for the daemon to exit, and then exits after the P.start (), then adds the P.join () and then the time. Sleep (1) Delete
I hope this article will help you with your Python programming.