Multithreading in python is not really true multi-threaded, if you want to get the most from multicore CPU resources, in most cases require the use of multiple processes in python. python provides a very easy to use multi-process packages Multiprocessing, only need to define a function, python does all the other things. With this package, you can easily complete the conversion from single-process to be executed concurrently. multiprocessing support the child, communicate and share data and synchronize different forms, provides Process, Queue, Pipe, LocK and other components
A, Process
语法:Process([group[,target[,name[,args[,kwargs]]]]])
Definition: target represents the calling object; args parameter indicates the position of the ancestral calling object; kwargs represent the calling object dictionary. name as an alias, groups will not actually called.
Methods: is_alive ():
join(timeout):
run():
start():
terminate():
Properties: authkey, daemon (through start () is provided), exitcode (process runtime None, if -N, representing the end signal N), name, pid. Where the daemon is automatically terminated after the termination of the parent process, and they can not generate a new process must be set before the start ().
1. Create a function, and as a single process
from multiprocessing import Process
def func(name):
print("%s曾经是好人"%name)
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
p.start() #start()通知系统开启这个进程
2. Create a function and as more processes
from multiprocessing import Process
import random,time
def hobby_motion(name):
print('%s喜欢运动'% name)
time.sleep(random.randint(1,3))
def hobby_game(name):
print('%s喜欢游戏'% name)
time.sleep(random.randint(1,3))
if __name__ == "__main__":
p1 = Process(target=hobby_motion,args=('付婷婷',))
p2 = Process(target=hobby_game,args=('科比',))
p1.start()
p2.start()
Results of the:
付婷婷喜欢运动
科比喜欢游戏
3. The process defined as a class (Another way to start a process, not very common)
from multiprocessing import Process
class MyProcess(Process):
def __init__(self,name):
super().__init__()
self.name = name
def run(self): #start()时,run自动调用,而且此处只能定义为run。
print("%s曾经是好人"%self.name)
if __name__ == "__main__":
p = MyProcess('kebi')
p.start() #将Process当作父类,并且自定义一个函数。
4.daemon program contrast effect
Without daemon property
import time
def func(name):
print("work start:%s"% time.ctime())
time.sleep(2)
print("work end:%s"% time.ctime())
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
p.start()
print("this is over")
#执行结果
this is over
work start:Thu Nov 30 16:12:00 2017
work end:Thu Nov 30 16:12:02 2017
Plus daemon property
from multiprocessing import Process
import time
def func(name):
print("work start:%s"% time.ctime())
time.sleep(2)
print("work end:%s"% time.ctime())
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
p.daemon = True #父进程终止后自动终止,不能产生新进程,必须在start()之前设置
p.start()
print("this is over")
#执行结果
this is over
Set the method of executing the daemon attribute want:
import time
def func(name):
print("work start:%s"% time.ctime())
time.sleep(2)
print("work end:%s"% time.ctime())
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
p.daemon = True
p.start()
p.join() #执行完前面的代码再执行后面的
print("this is over")
#执行结果
work start:Thu Nov 30 16:18:39 2017
work end:Thu Nov 30 16:18:41 2017
this is over
5.join (): After the above code is completed before execution code of the i-th surface.
Look at an example:
from multiprocessing import Process
import time,os,random
def func(name,hour):
print("A lifelong friend:%s,%s"% (name,os.getpid()))
time.sleep(hour)
print("Good bother:%s"%name)
if __name__ == "__main__":
p = Process(target=func,args=('kebi',2))
p1 = Process(target=func,args=('maoxian',1))
p2 = Process(target=func,args=('xiaoniao',3))
p.start()
p1.start()
p2.start()
print("this is over")
Results of the:
this is over #最后执行,最先打印,说明start()只是开启进程,并不是说一定要执行完
A lifelong friend:kebi,12048
A lifelong friend:maoxian,8252
A lifelong friend:xiaoniao,6068
Good bother:maoxian #最先打印,第二位执行
Good bother:kebi
Good bother:xiaoniao
Add join ()
from multiprocessing import Process
import time,os,random
def func(name,hour):
print("A lifelong friend:%s,%s"% (name,os.getpid()))
time.sleep(hour)
print("Good bother:%s"%name)
start = time.time()
if __name__ == "__main__":
p = Process(target=func,args=('kebi',2))
p1 = Process(target=func,args=('maoxian',1))
p2 = Process(target=func,args=('xiaoniao',3))
p.start()
p.join() #上面的代码执行完毕之后,再执行后面的
p1.start()
p1.join()
p2.start()
p2.join()
print("this is over")
print(time.time() - start)
#执行结果
A lifelong friend:kebi,14804
Good bother:kebi
A lifelong friend:maoxian,11120
Good bother:maoxian
A lifelong friend:xiaoniao,10252 #每个进程执行完了,才会执行下一个
Good bother:xiaoniao
this is over
6.497815370559692 #2+1+3+主程序执行时间
A change of position
from multiprocessing import Process
import time,os,random
def func(name,hour):
print("A lifelong friend:%s,%s"% (name,os.getpid()))
time.sleep(hour)
print("Good bother:%s"%name)
start = time.time()
if __name__ == "__main__":
p = Process(target=func,args=('kebi',2))
p1 = Process(target=func,args=('maoxian',1))
p2 = Process(target=func,args=('xiaoniao',3))
p.start()
p1.start()
p2.start()
p.join() #需要2秒
p1.join() #到这时已经执行完
p2.join() #已经执行了2秒,还要1秒
print("this is over")
print(time.time() - start)
#执行结果
A lifelong friend:kebi,13520
A lifelong friend:maoxian,11612
A lifelong friend:xiaoniao,17064 #几乎是同时开启执行
Good bother:maoxian
Good bother:kebi
Good bother:xiaoniao
this is over
3.273620367050171 #以最长时间的为主
6. Other Properties and Methods
from multiprocessing import Process
import time
def func(name):
print("work start:%s"% time.ctime())
time.sleep(2)
print("work end:%s"% time.ctime())
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
p.start()
p.terminate() #将进程杀死,而且必须放在start()后面,与daemon的功能类似
#执行结果
this is over
from multiprocessing import Process
import time
def func(name):
print("work start:%s"% time.ctime())
time.sleep(2)
print("work end:%s"% time.ctime())
if __name__ == "__main__":
p = Process(target=func,args=('kebi',))
# p.daemon = True
print(p.is_alive())
p.start()
print(p.name) #获取进程的名字
print(p.pid) #获取进程的pid
print(p.is_alive()) #判断进程是否存在
print("this is over")