1-3 join method

Process join a method of object

During the primary process if you want to run concurrently perform other tasks, we can open a child process, the child process tasks and task at this time of the main process of the two cases

Case 1: In the task after task with a child process of the main process independently of each other, the main task before the process is finished, the main process need to wait for the child process is finished, then uniform recycling resources.

Case 2: If the main task in the implementation process to a certain stage, it is necessary to wait for the child process is finished you can proceed, we need to have a mechanism that allows the main process to detect whether a child process has finished running, after the child process is finished continue, otherwise obstruction has been in place, this is the role of the join method

from multiprocessing import Process
import time
import random
import os

def task():
    print('%s is piaoing' %os.getpid())
    time.sleep(random.randrange(1,3))
    print('%s is piao end' %os.getpid())

if __name__ == '__main__':
    p=Process(target=task)
    p.start()
    p.join() #等待p停止,才执行下一行代码
    print('主')

With the join, the program is not a serial yet? ? ?

from multiprocessing import Process
import time
import random

def task(name):
    print('%s is piaoing' %name)
    time.sleep(random.randint(1,3))
    print('%s is piao end' %name)

if __name__ == '__main__':
    p1=Process(target=task,args=('egon',))
    p2=Process(target=task,args=('alex',))
    p3=Process(target=task,args=('yuanhao',))
    p4=Process(target=task,args=('wupeiqi',))

    p1.start()
    p2.start()
    p3.start()
    p4.start()

    # 有的同学会有疑问: 既然join是等待进程结束, 那么我像下面这样写, 进程不就又变成串行的了吗?
    # 当然不是了, 必须明确:p.join()是让谁等?
    # 很明显p.join()是让主线程等待p的结束,卡住的是主进程而绝非子进程p,
    p1.join()
    p2.join()
    p3.join()
    p4.join()

    print('主')

Detailed analysis as follows:

As long as the process will start at the beginning of the run, so when p1-p4.start (), the system has a process of four concurrent

And we p1.join () is waiting for the end of p1, p1 right does not end as long as the main thread would have been stuck in place, which is the crux of the problem

is to join the main thread, etc., and p1-p4 is still executed concurrently, p1.join when the rest p2, p3, p4 is still running, such as the end of the # p1.join, may p2, p3, p4 already ended such p2.join, p3.join.p4.join detected directly, without waiting

So the total time it takes to join 4 is still the longest time-consuming processes running

Start the process and join the above process can be abbreviated as

p_l=[p1,p2,p3,p4]

for p in p_l:
    p.start()

for p in p_l:
    p.join()

Methods or other properties of the two objects Process

Other methods of a process object: terminate and is_alive

from multiprocessing import Process
import time
import random

def task(name):
    print('%s is piaoing' %name)
    time.sleep(random.randrange(1,5))
    print('%s is piao end' %name)

if __name__ == '__main__':
    p1=Process(target=task,args=('egon',))
    p1.start()

    p1.terminate()#关闭进程,不会立即关闭,所以is_alive立刻查看的结果可能还是存活
    print(p1.is_alive()) #结果为True

    print('主')
    print(p1.is_alive()) #结果为False

Other properties of the process objects: name and pid

from multiprocessing import Process
import time
import random

def task(name):
    print('%s is piaoing' %name)
    time.sleep(random.randrange(1,5))
    print('%s is piao end' %name)

if __name__ == '__main__':
    p1=Process(target=task,args=('egon',),name='子进程1') #可以用关键参数来指定进程名
    p1.start()

    print(p1.name,p1.pid,)

Guess you like

Origin www.cnblogs.com/shibojie/p/11664751.html