Multithreading in python is not really multithreading. If you want to make full use of multi-core CPU resources, you need to use multiple processes in most cases in python. Python provides a very useful multi-process package Multiprocessing. You only need to define a function, and Python will do everything else. With this package, you can easily complete the conversion from single process to concurrent execution. multiprocessing supports sub-processes, communication and sharing of data, performs different forms of synchronization, and provides components such as Process, Queue, Pipe, LoCK, etc.
One, Process
Syntax: Process([group[,target[,name[,args[,kwargs]]]]])
Parameter meaning: target represents the calling object; args represents the positional parameter ancestor of the calling object; kwargs represents the dictionary of the calling object. name is an alias, groups will not actually be called.
Method: is_alive():
join(timeout):
run():
start():
terminate():
Attributes: authkey, daemon (to be set by start()), exitcode (None when the process is running, if it is -N, it means that it is terminated by signal N), name, pid. The daemon is automatically terminated after the parent process terminates, and it cannot generate a new process by itself, and must be set before start().
1. Create a function as a single process
from multiprocessing import Process
def func(name):print("%s used to be a good person"%name)if __name__ =="__main__":
p =Process(target=func,args=('kebi',))
p.start() #start()Notify the system to start this process
2. Create function and use it as multiple processes
from multiprocessing import Process
import random,time
def hobby_motion(name):print('%s like sports'% name)
time.sleep(random.randint(1,3))
def hobby_game(name):print('%s like games'% name)
time.sleep(random.randint(1,3))if __name__ =="__main__":
p1 =Process(target=hobby_motion,args=('Fu Tingting',))
p2 =Process(target=hobby_game,args=('Kobe',))
p1.start()
p2.start()
Results of the:
Fu Tingting likes sports
Kobe loves games
3. Define the process as a class (another way to start the process, not very commonly used)
from multiprocessing import Process
classMyProcess(Process):
def __init__(self,name):super().__init__()
self.name = name
def run(self): #start()When, run is automatically called, and it can only be defined as run here.
print("%s used to be a good person"%self.name)if __name__ =="__main__":
p =MyProcess('kebi')
p.start() #Treat Process as a parent class and customize a function.
4. Daemon program comparison effect
No daemon attribute
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")
# Results of the
this is over
work start:Thu Nov 3016:12:002017
work end:Thu Nov 3016:12:022017
Add the daemon attribute
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 #After the parent process terminates, it automatically terminates. A new process cannot be generated. It must be started at start()Set before
p.start()print("this is over")
# Results of the
this is over
The method that you want to execute after setting the daemon attribute:
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() #After executing the previous code, execute the following
print("this is over")
# Results of the
work start:Thu Nov 3016:18:392017
work end:Thu Nov 3016:18:412017this is over
**5. join(): After the above code is executed, the next i-side code will be executed. **
Let's 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 #Last execution, print first, indicating that start() is just to start the process, not that it must be executed
A lifelong friend:kebi,12048
A lifelong friend:maoxian,8252
A lifelong friend:xiaoniao,6068
Good bother:maoxian #Print first, execute second
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() #After the above code is executed, execute the following
p1.start()
p1.join()
p2.start()
p2.join()print("this is over")print(time.time()- start)
# Results of the
A lifelong friend:kebi,14804
Good bother:kebi
A lifelong friend:maoxian,11120
Good bother:maoxian
A lifelong friend:xiaoniao,10252 #After each process is executed, the next one will be executed
Good bother:xiaoniao
this is over
6.497815370559692 #2+1+3+ Main program execution time
Change 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() #Takes 2 seconds
p1.join() #By this time it has been executed
p2.join() #It has been executed for 2 seconds, and it will take 1 second
print("this is over")print(time.time()- start)
# Results of the
A lifelong friend:kebi,13520
A lifelong friend:maoxian,11612
A lifelong friend:xiaoniao,17064 #Start execution almost simultaneously
Good bother:maoxian
Good bother:kebi
Good bother:xiaoniao
this is over
3.273620367050171 # Give priority to the longest time
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() #Kill the process, and it must be placed in start()Later, the function is similar to daemon
# Results of the
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) #Get the name of the process
print(p.pid) #Get the pid of the process
print(p.is_alive()) #Determine whether the process exists
print("this is over")
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts