table of Contents
Several of usage and Daemon Process
A, Process of join usage
Man of few words said, directly on the code
join 用法一 ,单进程
from multiprocessing import Process
import time
def foo():
print('子进程 start')
time.sleep(2)
print('子进程 end')
if __name__ == '__main__':
p = Process(target=foo)
p.start()
# 核心需求就是想让子进程执行完,再执行主进程的print,time有弊端
# time.sleep(5)
p.join() # 在这阻塞住,主进程等待该子进程结束,然后再往下执行,(了解:内部会调用wait())
print('主进程')
# join用法二(并发)
from multiprocessing import Process
import time
def foo(x):
print('子进程 start')
time.sleep(x)
print('子进程 end')
if __name__ == '__main__':
p1 = Process(target=foo,args=(1,))
p2 = Process(target=foo,args=(2,))
p3 = Process(target=foo,args=(3,))
start = time.time()
p1.start()
p2.start()
p3.start() # 这几个相当于并发
# 核心需求就是想让子进程执行完,再执行主进程的print,time有弊端
# time.sleep(5)
p1.join() # 在这阻塞住,主进程等待该子进程结束,然后再往下执行,(了解:内部会调用wait())
p2.join()
p3.join()
# 总时长:按照最长的时间计算多一点
end = time.time()
print(end-start)
print('主进程')
# join用法三(串行)
from multiprocessing import Process
import time
def foo(x):
print('子进程 start')
time.sleep(x)
print('子进程 end')
if __name__ == '__main__':
p1 = Process(target=foo,args=(1,))
p2 = Process(target=foo,args=(2,))
p3 = Process(target=foo,args=(3,))
start = time.time()
p1.start()
p1.join() # 在这阻塞住,主进程等待该子进程结束,然后再往下执行,(了解:内部会调用wait())
p2.start()
p2.join()
p3.start() # 这几个相当于并行
p3.join()
# 核心需求就是想让子进程执行完,再执行主进程的print,time有弊端
# time.sleep(5)
# 总时长:按照最长的时间计算多一点
end = time.time()
print(end-start)
print('主进程')
# 优化join用法二
from multiprocessing import Process
import time
def foo(x):
print(f'子进程{x} start')
time.sleep(x)
print(f'子进程{x} end')
if __name__ == '__main__':
start = time.time()
p_list = []
for i in range(1,4):
p = Process(target=foo, args=(i,))
p.start()
p_list.append(p)
for p in p_list:
p.join()
end = time.time()
print(end-start)
print('主进程')
Two, Process and ppid usage of pid
We need to import the module os
Standing on the perspective of the current process: os.getpid () >>>> get pid the current process
os.getppid () pid >>>> Get the current process of the parent process
The child objects .pid >>>> get child process pid the current process
Specifically look at the following code
from multiprocessing import Process,current_process
import time,os
def task():
print('子进程 start')
print('在子进程中查看自己的pid',current_process().pid) # 在子进程查看自己pid方法一
print('在子进程中查看自己的pid',os.getpid()) # 在子进程查看自己pid方法二
print('在子进程中查看父进程的pid',os.getppid())
time.sleep(2)
print('子进程 end')
if __name__ == '__main__':
p = Process(target=task)
p.start()
print('在主进程查看子进程的pid',p.pid) # 获取pid,一定要写在start()之后
print('主进程的pid',os.getpid())
print('主进程的父进程pid',os.getppid())
print('主进程')
In the main process to see the child process pid 1928
pid 2900 the main process of
the parent process of the main process pid 1008
primary process
child process start
to see in the child own pid 1928
to see in the child own pid 1928
to see the parent in the child 2900 pid process
child process end
Three, Process of name usage
This is used to view the process name
from multiprocessing import Process,current_process
import time
def foo():
print('子进程 start')
print('>>>>',current_process().name) # 获取当前进程名
time.sleep(2)
print('子进程 end')
if __name__ == '__main__':
p = Process(target=foo)
p.start()
print(p.name)
Process-1
sub-process start
Process-1
sub-process end
Four, is_alive usage of Process
Used to determine whether the process alive, the result is True or False
from multiprocessing import Process,current_process
import time
def foo():
print('子进程 start')
time.sleep(2)
print('子进程 end')
if __name__ == '__main__':
p = Process(target=foo)
p.start()
print(p.is_alive()) # 没运行完就活着 True
time.sleep(5)
print(p.is_alive()) # 代码运行完了就算死了 False
print('主进程')
True
child process start
sub-process End
False
primary process
Five, Process terminate usage of
The process used to directly terminate (die)
from multiprocessing import Process,current_process
import time
def foo():
print('子进程 start')
time.sleep(50)
print('子进程 end')
if __name__ == '__main__':
p = Process(target=foo)
p.start()
p.terminate() # 给操作系统发了一个终止进程的请求
print(p.is_alive()) # 没运行完就活着 True
p.join() # 可以不等50秒,直接执行下面的
print(p.is_alive())
print('主进程')
True
False
primary process
Sixth, the daemon
Daemon nature is also a sub-code process, the main process is finished, the daemon directly end
from multiprocessing import Process
import time
def foo():
print('守护进程 start')
time.sleep(10)
print('守护进程 end')
if __name__ == '__main__':
p = Process(target=foo)
p.daemon = True # 把这个子进程定义为了守护进程,只陪伴到打印主进程就结束
p.start()
print('主进程') # 这个执行完,主进程的代码就执行完毕,子进程就直接结束,
Primary process
Seven, grab votes applet
from multiprocessing import Process
import json,time,os
def search():
time.sleep(1)
with open('db',mode='rt',encoding='utf-8') as fr:
res = json.load(fr)
print(f'还剩{res["count"]}张')
def get():
with open('db',mode='rt',encoding='utf-8') as fr:
res = json.load(fr)
time.sleep(1) # 模拟网络io
if res['count'] > 0:
res['count'] -= 1
with open('db',mode='wt',encoding='utf-8') as fw:
json.dump(res,fw)
time.sleep(1.5) # 模拟网络io
print(f'进程{os.getpid()}抢票成功')
else:
print('票已售罄!!!')
def task():
search()
get()
if __name__ == '__main__':
for i in range(15):
p = Process(target=task)
p.start()
p.join()
# 为了保证数据的安全,要牺牲掉效率