1, process and thread
Process advantages: Simultaneous use of multiple CPUs to perform multiple operations at the same time high efficiency
Process disadvantage: wasting memory
Thread Benefits: Shared memory, IO operation can be concurrent
Thread Disadvantage: preemption Resource
The process is not the more the better the best = CPU
The more threads are not the better. Specific case specific analysis request Context switch good
The smallest unit of execution of a task in a computer is a thread
IO-intensive (no CPU):
Multithreading
Compute intensive (with CPU):
Multi-process
To create a thread:
Import Threading def F1 (a): Pass = Threading. Thread (target=f1,args= (1,)) T.start ()
Thread Lock:
For example, the list "0,1,2,3,4,5,6,7,8,9", 1 threads from the previous read 2 threads from the back to the next modification will cause a task error, so there is a thread lock
Allows one thread to perform operations at the same time.
General use Rlock
import threading import timegl_num = 0lock = threading. Rlock () #创建Rlock对象 "lock def Func (): lock.a Cquire () #lock. Acqui Locking global Gl_num Gl_num + = 1 Time.sleep ( 1) print (Gl_num) lock.release () #lock. Release Close Lock for i in range (10 = Threading. Thread (Target=func) T.start ()
Event events Threading. Event call has three functions wait traffic lights clear red set green light
Import Threading
defDo (event):Print('Start') event.wait ()Print('Execute') Event_obj=Threading. Event () forIinchRange (10): T= Threading. Thread (Target=do, args=(Event_obj,)) T.start () event_obj.clear () InP= Input ('Input:')ifINP = ='true': Event_obj.set ()
Queue Module
The queue is the queues, it is thread-safe
Characteristics of the queue: FIFO
ImportQueueq= Queue. Queue (maxsize=0)#constructs a FIFO queue, maxsize specifies the queue length, which is 0 o'clock, which indicates that the queue length is infinite. Q.join ()#wait until the queue is none, and then perform another operationQ.qsize ()#returns the size of the queue (unreliable)Q.empty ()#returns True if the queue is empty, otherwise false (unreliable)Q.full ()#returns True when the queue is full, otherwise false (unreliable)Q.put (item, Block=true, Timeout=none)#put item in the queue tail, item must exist, can parameter block default is True, indicating that when the queue is full, waiting for the queue to give the available location #is non-blocking when flase, a queue is raised if the queues are full. Full exception. The parameter timeout can be selected to indicate the time that will be blocked, #If the queue cannot give the location where item is placed, the queues are raised. Full exception. Q.get (Block=true, Timeout=none)#When you remove and return a value from the header of the queue, the optional parameter block defaults to True, indicating that when the value is fetched, if the queue is empty, it is blocked if it is false, #if the queue is empty at this point, queue is raised. Empty exception. Optional parameter timeout, which indicates that the setting is blocked, and then, if the queue is empty, throws an empty exception. Q.put_nowait (item)#equivalent put (item,block=false)Q.get_nowait ()#unequal equivalent to get (item,block=false)
To create a process:
Import multiprocessing
Def f1 (a):
Pass
if __name__ = = ' __main__ ': #win系统下创建进程需要写
p = multiprocessing. Process (target=f1,args= (1,))
P.start ()
There is no sharing of data data between processes
However, you can use the method to share data between processes
m = multiprocessing. Manager ()
DIC = M.dict ()
Enables inter-process data sharing to show that data needs to be added to the dictionary in DIC
Import Multiprocessing def F1 (i,dic): = i+100 print(DIC)if__name__'__main__' : = multiprocessing. Manager () = m.dict () for in range : = Multiprocessing. Process (target=f1,args=(I,dic,)) P.start () p.join ()
Process Pools Pool
Pool = Pool (5) to set up a thread pool with a maximum of five processes at the same time
Pool.apply (func=f1,args= (i)) in the thread pool set process, apply, is queued,
Pool.apply_async (func=f1,args= (i)) above is only apply_async concurrency, there is no jion inside, so you need to set close below, first close and then jion to indicate that the thread is closed and then closed. Thread tasks do not go through the main threads.
Pool.apply_async Process Daemon =true
fromMultiprocessingImportPoolImport TimedefF1 (A): Time.sleep (1) Print(a)defF2 ():Passif __name__=='__main__': Pool= Pool (5) forIinchRange (10): Pool.apply (func=f1,args= (i,))
fromMultiprocessingImportPoolImport TimedefF1 (A): Time.sleep (1) Print(a)defF2 ():Passif __name__=='__main__': Pool= Pool (5) forIinchRange (50): Pool.apply_async (func=f1,args=(i,)) Pool.close () Pool.join ( )
Pool.apply_async () can optionally set the callback function to assign the return value to the set callback function
fromMultiprocessingImportPoolImport TimedefF1 (A): Time.sleep (1) returnadefF2 (a):Print(a)if __name__=='__main__': Pool= Pool (5) forIinchRange (10): Pool.apply_async (func=f1,args= (i,), callback=F2) Pool.close () Pool.join ()
Thread Process Learning