Python multitasking programming - process waiting
Python multitasking programming introduction:
The main process exits and the child process is destroyed
The main process will wait for all child processes to finish executing before exiting.
Target:
The main process exits and the child process is destroyed
Solution:
1. Set the child process as the daemon master process, and the master process exits the child process and destroys it directly
by sub_process.daemon = True
import multiprocessing
import time
def task ( ):
while True :
print ( "waiting..." )
time.sleep ( 1 ) _ _
# Standard python writing, directly executed modules, need to add code to judge whether it is the main module
if __name__ == '__main__' :
# create child process
sub_process = multiprocessing.Process ( target = task ) _ _
# Set the child process as the main daemon process, and then the main process exits the child process and destroys it directly
sub_ process.daemon _ _ = True
sub_process.start ( ) _ _
#Main process delay 5 seconds
time.sleep ( 5 ) _ _
print ( "over" )
operation result:
2. Python multitasking programming,Before exiting the main process , let the child process destroy it
sub_ process.terminate ()
import multiprocessing
import time
def task ( ):
# for i in range( 10):
while True :
print ( "waiting..." )
time.sleep ( 1 ) _ _
# Standard python writing, directly executed modules, need to add code to judge whether it is the main module
if __name__ == '__main__' :
# create child process
sub_process = multiprocessing.Process ( target = task ) _ _
sub_process.start ( ) _ _
#Main process delay 10 seconds
time.sleep ( 10 ) _ _
# Before exiting the main process, let the child process be destroyed
sub_process.terminate ( ) _ _
print ( "over" )
operation result:
Python multitasking programming :Process wait purpose:
The main process will wait for all child processes to finish executing before exiting.
Target:
The main process exits and the child process is destroyed
Solution:
1. Set the child process as the daemon master process, and the master process exits the child process and destroys it directly
by sub_process.daemon = True
import multiprocessing
import time
def task ( ):
while True :
print ( "waiting..." )
time.sleep ( 1 ) _ _
# Standard python writing, directly executed modules, need to add code to judge whether it is the main module
if __name__ == '__main__' :
# create child process
sub_process = multiprocessing.Process ( target = task ) _ _
# Set the child process as the main daemon process, and then the main process exits the child process and destroys it directly
sub_ process.daemon _ _ = True
sub_process.start ( ) _ _
#Main process delay 5 seconds
time.sleep ( 5 ) _ _
print ( "over" )
operation result:
2. Python multitasking programming,Before exiting the main process , let the child process destroy it
sub_ process.terminate ()
import multiprocessing
import time
def task ( ):
# for i in range( 10):
while True :
print ( "waiting..." )
time.sleep ( 1 ) _ _
# Standard python writing, directly executed modules, need to add code to judge whether it is the main module
if __name__ == '__main__' :
# create child process
sub_process = multiprocessing.Process ( target = task ) _ _
sub_process.start ( ) _ _
#Main process delay 10 seconds
time.sleep ( 10 ) _ _
# Before exiting the main process, let the child process be destroyed
sub_process.terminate ( ) _ _
print ( "over" )
operation result:
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00