Python daemon usage Example Analysis
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:
?
|
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
From multiprocessing import Process, current_process
Def 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.