Refer to the website

Blog.csdn.net/weixin_3711…

1/ The difference between the two

Apply (): blocks the main process, non-asynchronous (the children are not executed simultaneously) It is non-asynchronous by executing the children one by one, and it blocks by continuing to execute the main process code after apply() after all the children have finished executing. Apply_async () : non-blocking main process, asynchronous. It is non-blocking in that it does not wait for the child process to complete, the main process will continue to execute, and it will switch between processes according to the system schedule. To block the main process, use the.join() function. It is asynchronous in that the child processes are executed simultaneously.Copy the code

2/apply()

import time
import multiprocessing

def doIt(num) :
    print("Process num is : %s" % num)
    time.sleep(1)
    print('process %s end' % num)
        
if __name__ == '__main__':
    print('mainProcess start')
    Record the start time of execution
    start_time = time.time()
    Create three child processes
    pool = multiprocessing.Pool(3)
    print('Child start')
    for i in range(3):
            pool.apply(doIt,[i])
    print('mainProcess done time:%s s' % (time.time() - start_time))

Copy the code
Results as follows: from the result we can see that in the main process after an execution, create three child also immediately start, main process was blocked, and three child process is one by one in order to perform, wait until the child after all has been completed, the process will continue, and print out the last sentence. So, sure enough, the apply() function blocks the main forward and is non-asynchronous.Copy the code

3/apply_async()

Now I'm going to use apply_async(), just change the code up here to apply_async() where apply() is used, and I'm not going to stick the code up here so let's look at the result, and you can see, Apply_async () does not block, apply_async() does not block, apply_async() does not execute, apply_async() does not block. Verified that he was completed according to the system scheduling, why would it be so? The reason is that process switching is controlled by the operating system. We run the main process first, and the CPU runs so fast that the main process is finished and exits the program before the system dispatches the child thread. So the child process is not running.Copy the code

Can't apply_async() execute child processes? Definitely can!! What are you thinking, little brother? Remember what join() does? It can block the main process and wait for all its children to finish before running. Join () tells the main process to run its children and wait.Copy the code
import time
import multiprocessing

def doIt(num) :
    print("Process num is : %s" % num)
    time.sleep(1)
    print('process %s end' % num)
    
if __name__ == '__main__':
    print('mainProcess start')
    Record the start time of execution
    start_time = time.time()
    Create three child processes
    pool = multiprocessing.Pool(3)
    print('Child start')
    for i in range(3):
            pool.apply_async(doIt,[i])

    pool.close()
    pool.join()

    print('mainProcess done time:%s s' % (time.time() - start_time))
Copy the code
The result is as follows: We can see that even apply_async(), which does not block the main process, allows the children to run. In this case, the children run in alternating order, and the CPU executes the first child before the first child has finished. The system dispatches to a second child, and so on, running the children, terminating the children one by one, and finally running the main process, and we can see that apply_async() is much more efficient, The last sentence takes time to execute, which is why apply_async() is officially recommendedCopy the code