Python多进程
安装:
pip install multiprocess
使用:
import multiprocessing
创建进程:
multiprocessing.Process([group [, target [, name [, args [, kwargs]]]]])
# target表示调用对象
# args表示调用对象的位置参数元组
# kwargs表示调用对象的字典
# name为别名
# group实质上不使用
方法:
is_alive() :进程是否存活
join([timeout])
run()
start() :启动一个进程
terminate()
属性:authkey、daemon(要通过start()设置),exitcode(进程在运行时为None、如果为–N,表示被信号N结束)、name、pid。其中daemon是父进程终止后自动终止,且自己不能产生新进程,必须在start()之前设置。
创建一个单进程:
import multiprocessing
import time
def func():
for i in range(5):
print(i,"*"*10)
time.sleep(2)
if __name__ == '__main__':
p=multiprocessing.Process(target=func)
p.start()
print("p.pid:",p.pid)
print("p.name",p.name)
print("p.is_alive",p.is_alive())
# p.pid: 11792
# p.name Process-1
# p.is_alive True
# 0 **********
# 1 **********
# 2 **********
# 3 **********
# 4 **********
2.创建函数并将其作为多进程
import multiprocessing
import time
def func1():
for i in range(5):
print(i,"*"*10)
time.sleep(2)
def func2():
for i in range(5):
print(i*10,"*"*10)
time.sleep(2)
if __name__ == '__main__':
p1=multiprocessing.Process(target=func1)
p1.start()
p2 = multiprocessing.Process(target=func2)
p2.start()
print("The number of CPU is:" + str(multiprocessing.cpu_count()))
# The number of CPU is:2
# 0 **********
# 0 **********
# 1 **********
# 10 **********
# 2 **********
# 20 **********
# 3 **********
# 30 **********
# 4 **********
# 40 **********
将进程定义为类
import multiprocessing
import time
class Mytest(multiprocessing.Process):
def __init__(self,interval):
multiprocessing.Process.__init__(self)
self.interval=interval
def run(self):
n = 5
while n > 0:
print("the time is {0}".format(time.ctime()))
time.sleep(self.interval)
n -= 1
if __name__ == '__main__':
p=Mytest(2)
p.start()
# the time is Tue Apr 27 20:31:10 2021
# the time is Tue Apr 27 20:31:12 2021
# the time is Tue Apr 27 20:31:14 2021
# the time is Tue Apr 27 20:31:16 2021
# the time is Tue Apr 27 20:31:18 2021
设置daemon属性,主进程结束,子进程就随着结束
不加daemon:
import multiprocessing
import time
def func(sle):
print("time now_1:{}".format(time.ctime()))
time.sleep(sle)
print("time now_2:{}".format(time.ctime()))
if __name__ == '__main__':
p=multiprocessing.Process(target=func,args=(2,))
p.start()
print("end!")
# end!
# time now_1:Tue May 18 16:52:22 2021
# time now_2:Tue May 18 16:52:24 2021
加上daemon:
import multiprocessing
import time
def func(sle):
print("time now_1:{}".format(time.ctime()))
time.sleep(sle)
print("time now_2:{}".format(time.ctime()))
if __name__ == '__main__':
p=multiprocessing.Process(target=func,args=(2,))
p.daemon=True
p.start()
print("end!")
# end!
join让其中一个子进程运行,在这个子进程运行完毕,在让另外一个子进程运行,所以在其中一个子进程运行期间,其他进程只能按着固有的顺序等待。
import multiprocessing
import time
def funcA():
sle=2
print("funcA_time now_1:{}".format(time.ctime()))
time.sleep(sle)
print("funcA_time now_2:{}".format(time.ctime()))
return sle
def funcB():
t=funcA()
print("funcB_time now_1:{}".format(time.ctime()))
time.sleep(t)
print("funcB_time now_2:{}".format(time.ctime()))
if __name__ == '__main__':
p1=multiprocessing.Process(target=funcA)
p2=multiprocessing.Process(target=funcB)
p1.start()
p1.join()
p2.start()
p2.join()
print("end!")
#
# funcA_time now_1:Tue May 18 17:12:28 2021
# funcA_time now_2:Tue May 18 17:12:30 2021
# funcA_time now_1:Tue May 18 17:12:30 2021
# funcA_time now_2:Tue May 18 17:12:32 2021
# funcB_time now_1:Tue May 18 17:12:32 2021
# funcB_time now_2:Tue May 18 17:12:34 2021
# end!
互斥锁
加锁可以保证多个进程修改同一块数据时,同一时间只能有一个任务可以进行修改,即串行的修改
from multiprocessing import Lock,Process
import time
import os
def funcA(mutex):
sle=2
print("funcA_time now_1:{} pid:{}".format(time.ctime(),os.getpid()))
time.sleep(sle)
print("funcA_time now_2:{} pid:{}".format(time.ctime(),os.getpid()))
return sle
def funcB(mutex):
t=funcA(mutex)
print("funcB_time now_1:{}".format(time.ctime()))
time.sleep(t)
print("funcB_time now_2:{}".format(time.ctime()))
if __name__ == '__main__':
mutex=Lock()
p1=Process(target=funcA,args=(mutex,))
p2=Process(target=funcA,args=(mutex,))
p1.start()
print(p1.pid)
time.sleep(10)
p2.start()
print(p2.pid)
print("end!")
# 7292
# funcA_time now_1:Tue May 18 17:23:48 2021 pid:7292
# funcA_time now_2:Tue May 18 17:23:50 2021 pid:7292
# 20396
# end!
# funcA_time now_1:Tue May 18 17:23:58 2021 pid:20396
# funcA_time now_2:Tue May 18 17:24:00 2021 pid:20396
还没有评论,来说两句吧...