Process
Process definition: The process in progress is an abstraction of the running program.
1.multiprocessing Module Introduction
Multithreading in Python does not take advantage of multicore advantages, and if you want to fully use the resources of multicore CPUs (Os.cpu_count () Viewing), most of the situations in Python require multiple processes to be used. Python provides the multiprocessing.
The multiprocessing module is used to open sub-processes and perform our custom tasks (such as functions) in the subprocess, which is similar to the programming interface of the Multithreaded module threading.
The multiprocessing module has many functions: supporting sub-processes, communicating and sharing data, performing different forms of synchronization, and providing components such as process, Queue, Pipe, lock, etc.
One thing that needs to be emphasized again is that, unlike threads, processes do not have any shared state, the process modifies data, and changes are limited to that process.
2.process class
to create a process class :
Process ([group [, Target [, name [, args [, Kwargs]]]), an object instantiated by the class that represents a task in a child process (not yet started) emphasizes: 1. You need to use a keyword to specify parameter 2. args Specifies the positional parameter to be passed to the target function, which is a tuple form and must have a comma
Parameter Description:
1 The group parameter is not used, and the value is always None2 target to represent the calling object, that is, the task that the child process is performing 3 args represents the location of the calling object, the tuple, args= ("Egon",) 4 Kwargs represents the dictionary of the calling object, kwargs={' Name ': ' Egon ', ' Age ': 5} name for child process
Method Description:
1 P.start (): Starts the process and invokes the P.run () 2 P.run () in the subprocess: The method that runs at the start of the process, it is the function that calls target specified, we must implement the method in the class of the custom class 3 P.terminate ( ): Forcibly terminates the process p, does not do any cleanup operations, if p creates a child process, the child process is a zombie process, using this method requires special care of this situation. If P also holds a lock then it will not be released, resulting in a deadlock of 4 p.is_alive (): If P is still running, return true 5 p.join ([timeout]): The main thread waits for p to terminate (emphasis: is the main thread is in the state, and P is in the running state). Timeout is an optional time-out, and it should be emphasized that the p.join can only join the START process and not join the run-open process
Property Description:
1 P.daemon: The default value is False, if set to true, for p to run in the background daemon, when P's parent process terminates, p also terminates, and set to True, p cannot create its own new process, must be set before P.start () 2 p.name: Name of the process 3 p.pid: Process Pid4 P.exitcode: The process is none at run time, if it is –n, indicates the end of signal N (understand) 5 P.authkey: The authentication key of the process, by default is Os.urandom () A randomly generated 32-character string. The purpose of this key is to provide security for the underlying interprocess communication that involves a network connection, which can only succeed if you have the same authentication key (understand it)
3.process class
Note: the process () in Windows must be placed in # if __name__ = = ' __main__ ': under
two ways to create and open a child process
#开进程的方法一:
Import time import random from multiprocessing import Process def piao (name): Print ('%s piaoing '%name) Time.sleep (Random.randrange (1,5)) print ('%s Piao end '%name)
P1=process (target=piao,args= (' User1 ',)) #必须加, number P2=process (target=piao,args= (' User2 ',)) p3=process (target=piao,args= (' User3 ',)) p4=process (target=piao,args= (' User4 ',)) P1.start () P2.start () P3.start () P4.start () print (' main thread ') #开进程的方法二 import time
import random from multiprocessing import process class Piao (process): def __init__ (self,name): super (). __init__ () self.name=name def run (self): print ('%s piaoing '%self.name) time.sleep ( Random.randrange (1,5)) print ('%s Piao end '%self.name) P1=piao (' Egon ') P2=piao (' Alex ') p3= Piao (' Wupeiqi ') p4=piao (' Yuanhao ') P1.start () #start会自动调用run p2.start () P3.start () print (' main thread ')
4. Daemon Process
Master Process Creation Daemon
One: The daemon terminates after the execution of the main process code is completed
Second: The daemon can no longer open the child process, or throw an exception: Assertionerror:daemonic processes is not allowed to has children
Note: Processes are independent of each other, the main process code is running and the daemon terminates
Example:
From multiprocessing import Process
Import time import random class Piao (Process): def __init__ (self,name): self.name=name Super () . __init__ () def run (self): print ('%s is piaoing '%self.name) time.sleep (Random.randrange (1,3)) Print ('%s is Piao end '%self.name) P=piao (' Egon ') p.daemon=true #一定要在p. Start (), set p as daemon, prevent p from creating child processes, and the parent process Code execution ends, p terminates running p.start () print (' master ')
5. Queues
Processes are isolated from each other, and to implement interprocess communication (IPC), the Multiprocessing module supports two forms: queues and pipelines, both of which use message passing
Create a queue class (the underlying is implemented as a pipe and lock):
1 queue ([MaxSize]): Creates a shared process queue, which is a multi-process secure queue that enables data transfer between multiple processes using the queue.
Parameter Description:
1 MaxSize is the maximum number of items allowed in a queue, and no size limit is omitted.
Method Description:
Main methods:
1 The Q.put method is used to insert data into the queue, and the put method has two optional parameters: Blocked and timeout. If blocked is true (the default) and timeout is a positive value, the method blocks the time specified by timeout until the queue has the remaining space. If timed out, a Queue.full exception is thrown. If blocked is false, but the queue is full, an Queue.full exception is thrown immediately. 2 The Q.get method can read from the queue and delete an element. Similarly, the Get method has two optional parameters: Blocked and timeout. If blocked is true (the default) and timeout is a positive value, then no element is taken within the wait time, and a Queue.empty exception is thrown. If blocked is False, there are two cases where the queue has a value that is available and immediately returns the value, otherwise the Queue.empty exception will be thrown immediately if it is empty. 3 4 q.get_nowait (): Same as Q.get (False) 5 q.put_nowait (): Same as Q.put (False) 6 7 q.empty (): When calling this method, q is null to return true, and the result is unreliable, such as in the process of returning true, if the item is added to the queue. 8 Q.full (): Call this method when Q is full returns true, the result is unreliable, for example, in the process of returning true, if the items in the queue are taken away. 9 q.qsize (): Returns the correct number of current items in the queue, and the results are unreliable, for the same reason as Q.empty () and Q.full ()
Python path-process