Create a process using multiprocessing module

#_author: child star to 
#date: 2019/12/17
from multiprocessing Import Process
Import Time
Import os
two Method # two sub-processes will be called
DEF child_1 (i):
Print ( 'child process (% s) started, parent process of (% S) '% (os.getpid (), os.getppid ()))
t_start the time.time = ()
the time.sleep (I)
() = the time.time T_End
Print ( "child process (% s) execution time '% 0.2f' s "% (os.getpgid (), T_End-t_start))
DEF child_2 (I):
Print ( 'sub-process (% s) begins execution, the parent process of (% s) '% (os.getpid (), os.getppid ()))
t_start the time.time = ()
the time.sleep (I)
T_End the time.time = ()
Print ( "child process (% s) execution time'% 0.2f 's "% (os.getpgid (), T_End-t_start))
IF __name__ __ ==' __ main__ ':
Print (' ------- ------- parent process started ')
Print ( 'parent PID:% s'% os.getppid ( ))
p1 = Process (target = child_1, args = (1,)) # instantiation process P1
P2 = Process (target = child_2, args = (2,)) Example # Process P2
p1.start ()
p2.start ()
# At the same time the parent process is still down implementation, if p2 process is still running, will return True
Print ( "p1.is_alive =% S"% p1.is_alive ())
Print ( "p2.is_alive =% S"% p2. is_alive ())
# outputs p1, p2 and aliases process PID
Print ( "% S = p1.name" p1.name%)
Print ( '% S = p1.pid' p1.pid%)
Print ( "p2.name % S = "p2.name%)
Print ( '% S = p2.pid' p2.pid%)
Print ( 'wait for the child ------- -------')
p1.join ( )
p2.join ()
Print ( 'end ------- ------- parent process')
The result:
------- ------- parent process started
parent PID: 5580
p1.is_alive = True
P2.is_alive=True
p1.name=Process-1
2508 = p1.pid
p2.name = Process-2
p2.pid = 10164
------- ------- wait for the child


Guess you like

Origin www.cnblogs.com/startl/p/12054042.html