One, asynchronous + callback: Who is the thread who is empty, the process is the main process call
From concurrent.futures import Processpoolexcutor,threadpoolexecutor
From threading Import Current_thread
Import Requests,os,time,random
def get (URL):
Print ('%s GET%s '% (Current_thread (). Name,url))
Response=requests.get (URL)
Time.sleep (Random.randint (1,3))
If response.status_code=200:
Return Response.text
def pasrse (obj):
Res=obj.result ()
Print ('%s parsing result:%s '% (Current_thread (). Name,len (res)))
If __name__== ' __main__ ':
urls=[
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.baidu.com ',
' Https://www.python.org ',
]
Pool=threadpoolexecutor (4)
For URL in URLs:
Obj=pool.submit (Get,url)
Obj.add_done_callback (Pasrse)
Print (' main thread ', Current_thread (). Name)
Second, queue
Queue: FIFO
Q.put (1)
Q.put (2)
Q.put (3)
# Q.put (4)
Print (Q.get ())
Print (Q.get ())
Print (Q.get ())
Stack: Last in, first out
Q.put (' a ')
Q.put (' B ')
Q.put (' C ')
Print (Q.get ())
Print (Q.get ())
Print (Q.get ())
Priority queue: You can store values in the queue as a small tuple, the first element represents the priority, and the smaller the number the higher the priority
Q.put ((Ten, ' User1 '))
Q.put (( -3, ' user2 '))
Q.put (( -2, ' User3 '))
Print (Q.get ())
Print (Q.get ())
Print (Q.get ())
Third, the Event
From threading Import Event,current_thread,thread
Import time
Event=event ()
def check ():
Print ('%s ' is detecting service normal .... '%current_thread (). ')
Time.sleep (5)
Event.set ()
Def connect ():
Count=1
While not Event.is_set ():
if Count = = 4:
Print (' Too many attempts, please retry later ')
Return
Print ('%s attempted connection ... '% (Current_thread (). Name,count))
Event.wait (1)
Count+=1
Print ('%s start connection ... '% current_thread (). Name)
if __name__ = = ' __main__ ':
T1=thread (Target=connect)
T2=thread (Target=connect)
T3=thread (Target=connect)
C1=thread (Target=check)
T1.start ()
T2.start ()
T3.start ()
C1.start ()
Event.isset (): Returns the status value of the event;
Event.wait (): If Event.isset () ==false will block the thread;
Event.set (): Sets the status value of event to true, all the threads of the blocking pool are activated into a ready state, waiting for the operating system to dispatch;
Event.clear (): The status value of recovery event is false.
Iv.. Co-process
1, single-threaded implementation of concurrency: co-process
Multiple tasks that are referred to concurrently appear to be running concurrently
The nature of concurrent implementations: Toggle + Save State
Concurrent, parallel, serial:
Concurrency: Appears to be running simultaneously, toggle + save state
Parallel: The true sense of simultaneous operation, only in the case of multi-CPU can
Parallel implementation, 4 CPUs capable of parallel 4 tasks
Serial: One person finishes completing the entire execution before running the next task
From Greenlet import Greenlet
Import time
Def eat (name):
Print ('%s eat 1 '%name)
Time.sleep (30)
G2.switch (' Alex ')
Print ('%s eat 2 '%name)
G2.switch ()
def play (name):
Print ('%s play 1 '%name)
G1.switch ()
Print ('%s play 2 '%name)
G1=greenlet (EAT)
G2=greenlet (play)
G1.switch (' Egon ')
Day 32 asynchronous + callback, thread queue, thread event, coprocessor, single-threaded implementation experiencing IO switchover