Thread:
Queues: Queue
From queue import queue
Characteristics:
Advanced First Out
Self-locking, data security
Method:
Put () add
Get () gets
Put_nowait ()
Get_nowait ()
Full ()
Empty ()
Qsize ()
As with normal queue usage, see the process queue for details
Stack : lifoqueue
From queue import Lifoqueue
Last in, first out
Self-locking, data security
Priority Queue :P Riorityqueue
From queue import Priorityqueue
The Put ((TULP)) parameter accepts a tuple, which is two elements, the first an int that represents a priority, and the smaller the int, the greater the precedence
thread pool : from concurrent.futures import threadpoolexecutor
1, threading no thread pool related content, at the back of the time, someone wrote a concurrent.futures to help us manage thread/process pool
Process pools and thread pools in the Concurrent.futures module
Highly encapsulated
How the process/thread pool is used uniformly:
co-process :
Summary : On a thread basis, switch between multiple tasks
Saves thread-on-time consumption, and the co-process is dispatched from the Python code level.
yield:
1, the match between yield and next () is one of the most basic co-processes.
2, only switch between programs, no time to re-use any IO operation
Third-party module: Greenlen
From Greenlet import Greenlet
This module is used to help us to perform transitions between the threads.
Method:
Switch () toggle
co-process content
Third-party module: gevent
Import Gevent
This module is based on the Greenlen module developed by an auto-encounter block is switched module (this is the artifact)
At the same time, this mechanism takes full advantage of the idle time generated by IO operations. Make the program faster
Spawn (FU) set the FU function to co-process
Sleep () clogging
Join () Jam
Joinall ([list]) waits for all the threads in the list to complete and unblock.
Note : Gevent only know their own blockage status, if you want to let them know other needs with the help of monkey
Help Gevent recognize other blockages: Monkey.patch_all ()
From gevent import Monkey;monkey.patch_all () write before other modules are imported
Monkey.patch_all () is to record the blockage status of all the modules below
1 #use the co-process to reduce the time consumed by IO operations2 fromGeventImportMonkey;monkey.patch_all ()3 Importgevent4 Import Time5 6 defeat ():7 Print('Eat')8Time.sleep (2)9 Print('Finished eating')Ten One defPlay (): A Print('Play') -Time.sleep (1) - Print('It's a beautiful play.') the -G1 =gevent.spawn (Eat) -G2 =Gevent.spawn (play) - Gevent.joinall ([g1,g2]) + #G1.join () - #G2.join () + #not executed A #Why didn't you do??? Do you need to open it? at #It didn't turn on, but it switched. - #gevent help you to do the switch, do the switch is conditional, encountered IO to switch - #gevent does not recognize IO operations other than gevent in this module - #use join to block until the process task finishes - #help Gevent to understand blocking in other modules - #from gevent import Monkey;monkey.patch_all () write before other modules are imported
gevent
The queue of concurrent programming threads, the thread pool; And the co-process (iv)