Lottery
1. Process Class
from Import Process def func (name): Print ('hello', name) if __name__ ' __main__ ' : = Process (Target=func, args= ('bob',)) P.start () p.join ()
2. Context and Start method
Start method
ImportMultiprocessing as MPdeffoo (q): Q.put ('Hello')if __name__=='__main__': Mp.set_start_method ('Spawn') Q=MP. Queue () P= MP. Process (Target=foo, args=(q,)) P.start ()Print(Q.get ()) P.join ()
Context
ImportMultiprocessing as MPdeffoo (q): Q.put ('Hello')if __name__=='__main__': CTX= Mp.get_context ('Spawn') Q=CTX. Queue () P= CTX. Process (Target=foo, args=(q,)) P.start ()Print(Q.get ()) P.join ()
3. Inter-Process Exchange objects
Queues
from multiprocessing import Process, Queue def func (q): Q.put ([ , None, " hello ") if = = " __main__ ' p.join ()
Pipes
fromMultiprocessingImportProcess, Pipedeffunc (conn): Conn.send ([, None,'Hello']) conn.close ()if __name__=='__main__': Parent_conn, Child_conn=Pipe () p= Process (Target=func, args=(Child_conn,)) P.start ()Print(Parent_conn.recv ())#prints "[All, None, ' hello ']"P.join ()
4. Synchronization between processes
For example, you can use a lock to ensure that only one process prints to the standard output
fromMultiprocessingImportProcess, Lockdeffunc (lock, I): Lock.acquire ()Try: Print('Hello World', i)finally: Lock.release ()if __name__=='__main__': Lock=Lock () forNuminchRange (10): Process (Target=func, args= (lock, Num)). Start ()
5. Sharing status between processes
Shared Memory
With Value or Array, data can be stored in a shared memory map. For example, the following code
fromMultiprocessingImportProcess, Value, Arraydeff (N, a): N.value= 3.1415927 forIinchRange (len (a)): A[i]= -A[i]if __name__=='__main__': Num= Value ('D', 0.0) Arr= Array ('I', Range (10)) P= Process (Target=f, args=(num, arr)) P.start () P.join ()Print(Num.value)Print(arr[:])
The parameter ' d ' represents a double-precision floating-point number, and the parameter "I" represents a signed integer. These shared objects are process-and thread-safe.
Server process
Through Manager (), returns a Manager object that controls the service process that holds the Python object and allows other processes to manipulate them using a proxy.
The types of manager objects that are returned by the manager () are:List,Dict,Namespace,Lock,Rlock,Semaphore,Boundedsemaphore,Condition,Event,Barrier,Queue,ValueandArray.
fromMultiprocessingImportProcess, ManagerdefF (D, L): d[1] ='1'd['2'] = 2d[0.25] =None l.reverse ()if __name__=='__main__': With Manager () as Manager:d=manager.dict () L= Manager.list (Range (10)) P= Process (Target=f, args=(d, L)) P.start () P.join ( )Print(d)Print(l)
The service process Manager is more flexible than using shared memory objects because they can support arbitrary object types. Similarly, a manager can share the process over the network on different computers. However, they are slower than using shared memory.
6. Using Process Pools
fromMultiprocessingImportPool fromTimeImportSleepdeff (x):returnx*xif __name__=='__main__': #Start 4 worker processesWith Pool (processes=4) as Pool:#print "[0, 1, 4,..., Bayi]" Print(Pool.map (F, Range (10))) #Print same numbers in arbitrary order forIinchPool.imap_unordered (F, Range (10)): Print(i)#evaluate "F (Ten)" asynchronouslyres = Pool.apply_async (f, [10]) Print(Res.get (Timeout=1))#Prints " #Make worker sleep for ten secsres = Pool.apply_async (sleep, [10]) Print(Res.get (Timeout=1))#raises multiprocessing. Timeouterror #exiting the ' with '-block have stopped the pool
Python Multi-process