How to ensure that the child process exits at the same time without becoming an orphan process when the main process is killed (i)

Source: Internet
Author: User
Tags semaphore signal handler thread class

In Python, because of the existence of the global interpreter lock Gil, the multithreading in Python does not significantly improve the efficiency of the program (in this case, CPU-intensive), then in the processing of CPU-intensive computing, multi-process model to deal with, The multiprocessing library is available in the Python standard library to support the programming of multi-process models. The process class provided in multiprocessing is used by developers to write child processes that resemble the threading provided by the standard library. The thread class, which also provides the process pool class, reduces the overhead of process creation and destruction to improve reuse (see previous article).

In a multithreaded model, by default (Sub-thread.daemon=false) The main thread waits for a child thread to exit before exiting, and if Sub-thread.setdaemon (True), the main thread does not wait for the child thread to exit directly, In this case, the child thread exits with the main thread's alignment, avoiding the need for a child thread to join in the main thread, waiting for the child thread to finish executing before exiting. Correspondingly, in a multi-process model, the Process class also has the daemon attribute, and it represents a similar meaning to Thread.daemon, and when set sub-process.daemon=true, the child process is required to wait in the main process, Otherwise, the child process exits as the main process exits:

1 ImportThreading2 Import Time3 ImportMultiprocessing4 5 6 defFun (args):7      forIinchRange (100):8         Printargs9Time.sleep (1)Ten  One  A if __name__=='__main__': -Threads = [] -      forIinchRange (4): the         #t = Threading. Thread (Target=fun, args= (str (i),)) -         #T.setdaemon (True) -t = multiprocessing. Process (Target=fun, args=(str (i),)) -T.daemon =True + T.start () - threads.append (t) +  A      forIinchThreads: atI.join ()

Running the above code, the main process waits for the child process to exit after execution, ending the entire program. LINE15, 16 is multithreaded mode, running effect and multi-process similar. Note that the similarity here means that the program is running normally, and when there are human disturbances, such as when the process is started, after the process is killed by kill-9, the situation is different, we know that the multithreaded model is more complex, but also in the same process, killing the main process, All threads will exit with the main process exit, and in the multi-process model, each process is independent, after killing the main process, the other child processes will not be affected, and will continue to run, the above code in the process of the Targe function is very simple, only a limited number of cyclic output, and in the real scene, The child process may always be in the loop processing business, and if the child process is killed, the child process is not effectively recycled, it is necessary to kill manually, it is more troublesome.

In this case, the first thought is to use the signal signal to deal with, so that the killing of the main process can no longer use the kill-9 command, because the kill-9 command to send a Sigkill command to the process, while in the system, Sigkill and sigstop Two kinds of signals, The process cannot be captured and will exit immediately upon receipt. Execute kill-l under Linux, you can see all the semaphore, here using the sigterm signal, sigterm means the stop signal, is the KILL command transmission system default signal, it differs from Sigkiil is that sigterm more friendly, The process can capture the sigterm signal, and then do some cleanup work as needed, making some changes to the above code after this is clear:

1processes = []2 defFun (x):3     Print 'Current sub-process pid is%s'%os.getpid ()4      whileTrue:5         Print 'args is%s'%x6Time.sleep (1)7 8 9 defTerm (Sig_num, addtion):Ten     Print 'Terminate process%d'%os.getpid () One     Try: A         Print 'The processes is%s'%Processes -          forPinchprocesses: -             Print 'Process%d Terminate' the p.terminate () -             #Os.kill (, signal. SIGKILL) -     exceptException as E: -         PrintStr (e) +  -  + if __name__=='__main__': A     Print 'Current pid is%s'%os.getpid () at      forIinchRange (3): -t = Process (Target=fun, args=(str (i),)) -T.daemon =True - T.start () - processes.append (t) - signal.signal (signal. SIGTERM, term) in     Try: -          forPinchprocesses: to P.join () +     exceptException as E: -         PrintSTR (e)

Run the above code, output the main process ID, and then send the sigterm signal to the main process via the kill-15 PID, and terminate the child process before the main process exits. However, when exiting, Line32 captures exception information OSError: [Errno 4] interrupted system call, which indicates that the main process is signaled and exited when the child process joins. The program gets the expected result, when sending a sigterm signal to the main process, the first end of all child processes, and then the main process exits. Then use KILL-15 plus the process ID of the child process, send the sigterm signal to the child process to see if the child process can achieve the same effect, but after sending a signal to the process, does not enter the term function, through the PS can be seen, the child process received the sigterm signal, the self-exit, The main process and other child processes are not affected and are still functioning normally, and this does not get the same effect. We know that the child process inherits the signal processing mechanism of the parent process, but here the subprocess does not run the term function after receiving the SIGTERM signal, carefully observing the code example above, and noticing that the child process has started when registering the signal handler, so there is no signal processing function registered in the child process, and then, We modify the main process to ensure that the signal processing function is registered before the child process starts:

1 if __name__=='__main__':2 signal.signal (signal. SIGTERM, term)3     Print 'Current main-process pid is%s'%os.getpid ()4      forIinchRange (3):5t = Process (Target=fun, args=(str (i),))6T.daemon =True7 T.start ()8 processes.append (t)9     Ten     Try: One          forPinchprocesses: A P.join () -     exceptException as E: -         PrintSTR (e)

Run the program again, through the kill-15 to the parent process to send the sigterm signal, the process received a signal, but the program continues to run, observe the following output information, the main process received a signal, the execution of the term function, and by calling the child process of p.terminate (), Note that the Terminate is implemented under the Linux system as follows: Terminate the process. On Unix the-is-done using the SIGTERM signal, that is, when a child process calls P.terminate (), the SIGTERM signal is actually sent to the subprocess, Before we had placed the registration of the signal handler function before the child process was started, the child process was able to execute the signal processing function. From the output of the processes information can be seen, due to the boot order, the global processes variable is not well shared with the child process information. After receiving the semaphore sent by P.terminate (), the child process executes the term function, which attempts to kill the child process again by calling P.terminate (), thus entering an infinite loop, kill-15 sending a sigterm signal to the child process, will get the same result.

At this point, the basic understanding of how to send a semaphore to the main process to end the main process and its child process method, then there is no way to send a signal to the child process to achieve the same effect? The answer is yes, when we create a child process in the main process, the main process and the child process it creates belong to the same group, the concept of this grouping becomes a process group in Linux, it is a collection of one or more processes, and the process group IDs of the processes in the same process group are consistent. Using the Os.getpgid method in the Python standard library, the ID of the process is used to get the group ID of the process, and then the OS.KILLPG method is called to send a signal to the process's group ID, and now the above code is simply modified:

1 defFun (x):2     Print 'Current PID was%s, group ID is%s'%(Os.getpid (), Os.getpgrp ())3      whileTrue:4         Print 'args is%s'%x5Time.sleep (1)6 7 8 defTerm (Sig_num, addtion):9     Print ' currentpid is%s, group iD is%s'%(Os.getpid (), Os.getpgrp ())Ten OS.KILLPG (Os.getpgid (Os.getpid ()), signal. SIGKILL) One  A  - if __name__=='__main__': - signal.signal (signal. SIGTERM, term) the     Print 'Current pid is%s'%os.getpid () -      forIinchRange (3): -t = Process (Target=fun, args=(str (i),)) -T.daemon =True + T.start () - processes.append (t) +      A     Try: at          forPinchprocesses: - P.join () -     exceptException as E: -         PrintSTR (e)

Note In the code, in order to prevent an infinite loop from appearing before, in the term function, we send the sigkill signal directly to the process group via OS.KILLPG. Running the code, we can see from the output that, in the process group, the process group ID of the main process and the child process is the same as the PID of the main process. When a sigterm signal is sent to the main or child process through kill-15, both the process group master and child processes are killed:

How to ensure that the child process exits at the same time without becoming an orphan process when the main process is killed (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.