Python daemon usage instance analysis, python daemon
This example describes how to use the Python daemon. Share it with you for your reference. The specific analysis is as follows:
The daemon can always run without blocking the exit of the main program. To mark a daemon Process, you can set the daemon attribute 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 exits in 1 second, the print "Exiting: % s \ n" % p. name of the daemon cannot be seen.
To view the print "Exiting: % s \ n" % p. name statement output. You can use join to wait for the main process to exit after the daemon exits, that is, p. add p. join () and then time. Sleep (1) Delete
I hope this article will help you with Python programming.