This example describes the Python daemon usage. Share to everyone for your reference. The specific analysis is as follows:
The daemon is capable of running 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:
Import osimport timeimport randomimport sysfrom multiprocessing import process,current_processdef daemon (): p = Current_process () print "Starting id%d prccess%s\n"% (p.pid,p.name) Sys.stdout.flush () time.sleep (3) print "exiting:%s\n"% p.name Sys.stdout.flush () def main (): p = Process (name= "Daemon", Target=daemon) p.daemon=true P.start () if __name__== "__main__": Main () time.sleep (1)
Because the main process will exit after 1 seconds, the daemon's print "exiting:%s\n"% p.name is not visible.
If you want to see the print "exiting:%s\n"% p.name statement output of the daemon, you can take advantage of the join, the main process waits for the daemon to exit before exiting, that is, after P.start () add P.join () and then put time. Sleep (1) Delete
Hopefully this article will help you with Python programming.