一 Process对象的join方法
在主进程运行过程中,如果想并发地执行其它的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况:
- 情况一:在主进程的任务与子进程的任务彼此独立的情况下,主进程的任务先执行完毕后,主进程还需要等待子进程执行完毕,然后统一回收资源
- 情况二:如果主进程的任务在执行到某一个阶段时,需要等待子进程执行完后才能绩效执行,就需要有一种机制能够让主进程检测子进程是否运行完毕,在子进程执行完毕后才继续执行,否则一直在原地阻塞,这就是join方法的作用
|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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``(``'主进程'``)
|
有了join,程序不就是串行了吗??
|----------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from
multiprocessing ``import
Process
import
time
import
random
def
task(name):
``print``(``'%s is piaoing'
%
name)
``time.sleep(random.randrange(``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() ``# 等待p停止,才执行下一行代码
``p2.join()
``p3.join()
``p4.join()
``print``(``'主进程'``)
|
详细解析如下:
进程只要start就会在开始运行了,所以p1-p4.start()时,系统中已经有四个并发的进程了,而我们p1.join()是在等p1结束,没错p1只要不结束,主线程就会一直卡在原地,这也是问题的关键,join是让主线程等,而p1-p4仍然是并发执行的,p1.join的时候,其余p2,p3,p4仍然在运行,等p1.join结束,可能p2,p3,p4早已经结束了,这样p2.join,p3.join,p4.join直接通过检测,无需等待,所以4个join花费的总时间仍然是耗费时间最长的那个进程运行的时间
上述启动进程与join进程可以简写为
|---------------|-------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | p_l ``=
[p1, p2, p3, p4]
for
p ``in
p_l:
``p.start()
for
p ``in
p_l:
``p.join()
|
二 Process对象的其它属性和方法
进程对象的其它方法一:terminate与is_alive
|----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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
|
进程对象的其它属性:name和pid
|----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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)
|
三 练习题:
1、改下下列程序,分别实现下述打印效果
|-------------------------|-----------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 | # 效果一:保证最先输出-------->4
``p1.start()
``p2.start()
``p3.start()
``print``(``'------->4'``)
``p1.join()
|
|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | # 效果二:保证最后输出-------->4
``p1.start()
``p2.start()
``p3.start()
``p1.join()
``p2.join()
``p3.join()
``print``(``'------->4'``)
|
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 | # 效果三:保证按顺序输出
``p1.start()
``p1.join()
``p2.start()
``p2.join()
``p3.start()
``p3.join()
``print``(``'------->4'``)
|