One, yesterday content review 1. Concepts and theories
Process is the smallest unit of computer resource allocation
Process three-state, synchronous, asynchronous, blocking, non-blocking
2. Creation of processes
Instantiation, self-built class Run,start,join,terminate,daemon, etc.
3. Synchronization control of the process
Lock: Mutual exclusion lock
Semaphore: Lock + counter
Event: Events
4. Inter-process communication queue queue:put, get, empty, full, put_nowait, get_nowait pipe + lock
Data security between processes-process security
Can be any data type
Piping pipe:
With both ends, two-way communication
Need to shut down all the unused ports, will be in the Recv place error
Process not secure
5. Data sharing manager:dict,list
# processes are used on the same computer
# data is not secure between processes
Data sharing between processes currently in use: Message middleware
#memcache
#rabbitmq
#redis
6. Process Pools Pool
#什么情况下用进程池:
# High-CPU code requires process pooling
#进程池cpu个数 +1
# Pool
Apply Sync
Apply_async Asynchronous Commit
#get Get the return value
#close
#join
Map
# Apply_async's simplified version, which implements the close and join methods internally
#但是没有get方法, the return value cannot be accepted
callback function: Apply_async (callback=???)
# The callback function is actually executed in the main process
7, signal volume and process pool differences, the use of semaphores
#在同一时刻只会有n个进程在执行某段代码
#不同:
#信号量是有多少任务开启多少进程, the semaphore still brings a lot of burden to the operating system.
# The number of processes in the pool is fixed, just borrowing the process from the pool to perform the task
The use of semaphores (in the same target function, the high IO portion with multiple processes, the high CPU portion is calculated with semaphores, thus saving process switching overhead):
Two, thread 1. Thread Concepts and Features
Why does a process also have threads open:
A thread is the smallest unit in a computer that can be scheduled by the CPU:
Features of Multithreading: concurrency, lightweight, data not isolated
Multi-process Features: concurrency, cumbersome operation, data isolation
2. Thread opening
ImportJSONImportTime,os fromThreadingImportThreaddeffunc (): forIinchRange (10): Time.sleep (0.5) Print('Thread:', I,os.getpid ())if __name__=='__main__': T= Thread (target=func) T.start () Time.sleep (1) Print('In main 1', Os.getpid ()) Time.sleep (1) Print('In main 2', Os.getpid ())
the main thread and the child thread are in the same process
3. Efficiency test
fromMultiprocessingImportProcess fromThreadingImportThreadImport Timedeffunc (num):Print(num**num)if __name__=='__main__': P_lst=[] Start=time.time () forIinchRange (50): P= Process (Target=func, args=(i,)) P.start () P_lst.append (p) forIinchP_lst:i.join ()Print('======', Time.time ()-start) T_lst=[] Start=time.time () forIinchRange (50): T= Thread (Target=func, args=(i,)) T.start () T_lst.append (p) forIinchT_lst:i.join ()Print('********', Time.time ()-start)
Multithreading is more than a few orders of magnitude for high computational tasks
4. Data Isolation Testing
from Import =100def func (): Global n -=1 = Thread (target= Func) T.start () t.join ()print(n)
data isolation, thread sharing process resources
5. Sub-processes and master processes
from threading import Thread,currentthread import time def func (): Time.sleep ( 1) print (" child process " , CurrentThread ()) T = Thread (Target=func) T.start () print ( " Span style= "COLOR: #800000" > main process ", CurrentThread ()) # The main thread end means the master process ends, and the main line routines waits for the child thread to end
The main thread end means the master process is finished, and the main thread routines waiting for the child thread to end.6. Global Interpreter lock Gil
Python Full stack development day34-threading Thread