Python multi-process Understanding multiprocessing process join run

Source: Internet
Author: User

Recently saw the next multi-process.

A near-bottom implementation is to fork out the subprocess using the os.fork () method. But there are limitations to doing things like this. For example, there is no fork () method in Windows OS module.

Windows:. Linux:

There is also another module: subprocess. This is not the whole, but from Vamei's blog to see that there are also limitations.

So speak directly to the protagonist--- multiprocessing module. The multiprocessing module simulates the effect of f ork on Windows, and can be implemented across platforms, so mostof them use multiprocessing.

Here is a simple code to demonstrate the creation process:

#encoding: utf-8from multiprocessing import processimport os, time, random# Code blocks actually executed after thread startup Def r1 (Process_name):     for i in range (5):         print process_name, os.getpid ()      # Prints out the current process's Id        time.sleep (Random.random ()) DEF R2 (process_name) :     for i in range (5):         Print process_name, os.getpid ()       #打印出当前进程的id          time.sleep (Random.random ())     if __name__ ==  "__ main__ ":        print " Main process run ... "         p1 = process (target=r1, args= (' process_name1 ', ))         #target: Specifies the function that the process executes, args: The parameter of the function, which requires the use of tuple        p2 =  Process (target=r2, args= (' process_name2 ', ))                  p1.start ()      #通过调用start方法启动进程, similar to threads.         p2.start ()      #但run方法在哪呢? I'll tell you later ...         p1.join ()       #join方法也很有意思, and wondering all afternoon, Finally understood. We'll show you later.         p2.join ()         print   "Main process runned all lines ..."

Execution Result:

There are two methods mentioned above:run and join

Run: If you do not specify target when creating the process object, then the process's Run method will be executed by default:

#encoding: Utf-8from multiprocessing import processimport os, time, randomdef  r ():    print  ' Run method '     if __name__ ==   "__main__":        print  "Main process run ..."          #没有指定Process的targt          p1 = process ()         p2 = process ()           #如果在创建Process时不指定target, there is no effect on execution. Because the default run method is to determine if you do not specify target, then do nothing          #所以这里手动改变了run方法          p1.run = r        p2.run  = r                 P1.start ()        &nbsP;p2.start ()         p1.join ()          p2.join ()         print  "main process runned  all lines ... "

Another: Python source code, Process.run method:

Execution Result:

Visible if you do not specify targetwhen instantiating a process , the default run method is executed.

There is also a join method:

In the code shown above, two join methods were called after the start method of the process was called. What does this join method do?

The Official document means: block the current process until the process that calls the Join method finishes executing before continuing with the current process.

Like the code just now, just comment out the two joins:

#encoding: Utf-8from multiprocessing import processimport os, time, randomdef  R1 (process_name):     for i in range (5):         print process_name, os.getpid ()       #打印出当前进程的id          time.sleep (Random.random ()) def r2 (process_name):     for i in range (5):        print  Process_name, os.getpid ()       #打印出当前进程的id          time.sleep (Random.random ()) if __name__ ==  "__main__":         print  "Main process run ..."         p1  = process (target=r1, args= (' process_name1 ', ))           p2 = procESS (target=r2, args= (' process_name2 ', ))           P1.start ()         p2.start ()           #p1. Join ()          #p2. Join ()              print  "Main process runned all lines ..."

Execution Result:

The main process was found not to wait until two child processes were executed before proceeding. Instead, the two processes are started immediately after the execution of the process.

In order to understand deeply, this time P2 execution function inside sleep time, let him sleep a little more, and then keep P1 join, comment out P2 join, the effect is more obvious:

#encoding: Utf-8from multiprocessing import processimport os, time, randomdef  R1 (process_name):     for i in range (5):         print process_name, os.getpid ()       #打印出当前进程的id          time.sleep (Random.random ()) def r2 (process_name):     for i in range (5):        print  Process_name, os.getpid ()       #打印出当前进程的id          time.sleep (Random.random () * *) if __name__ ==  "__main__":         print  "Main process run ..."          p1 = process (target=r1, args= (' process_name1 ', ))           p2 = prOcess (target=r2, args= (' process_name2 ', ))           P1.start ()         p2.start ()          p1.join ()          #p2. Join ()              print  "Main process runned all lines ..."

Execution Result:

Discovering that the main thread is just waiting for P1 to complete, it will execute down without waiting for P2 to complete.

So the general way to use multiple processes is to start the process by calling start first, then calling join in turn to ask the main process to wait for the end of the child process.

But why call start first and then call join, instead of calling join when Start is finished , as follows:

By:

P1.start () P2.start () P1.join ()

Switch

P1.start () P1.join () P2.start ()

Execution effect:

The discovery is to execute P1 first, then execute the main thread, and finally start P2.

This morning has been puzzled this matter, now finally understood. Join is used to block the current thread, P1.start (), P1 will prompt the main thread, need to wait until the end of the P1 to execute, the main thread is waiting, naturally did not execute p2.start () This sentence, of course, has become the effect of the diagram.


Python multi-process Understanding multiprocessing process join run

Related Article

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: info-contact@alibabacloud.com 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.