Queues (queues within the same process, multithreading)
Import the queue queue. Queue () FIFO queue.lifoqueue () LIFO queue. Priorityqueue () Priority queue
Q = queue. Priorityqueue ()
Q.put () receives a tuple
The first parameter in the tuple is: Indicates the priority of the current data
The second parameter in a tuple is the data that needs to be stored in the queue
Precedence comparison (the type that represents the priority must be consistent)
If all are int, the size of the numerical value, the smaller
If all are str, compare the size of the string, (starting with the Ascaii code of the first string) and first select the Ascaii code.
Thread pool
Hold a fixed number of threads that wait for a task and automatically perform the task on the thread once it has a task.
The optimal number of threads max_workers = (OS. Cpu_count () or 1) * 5
fromConcurrent.futuresImportThreadpoolexecutor,processpoolexecutor#thread Async and process Async#Concurrent.futures This module is an asynchronous call mechanism#From multiprocessing Import Pool This module can be either asynchronous or synchronous,deffunc (num): Sum=0 forIinchrange (num): Sum+ = I**2Print(sum)if __name__=='__main__': P= Threadpoolexecutor (10) forNinchRange (100): #P.submit (func, args= (n,))P.submit (func, N)#Submit a TaskP.shutdown ()#The equivalent of close+join in a process pool is to have the parent thread wait for all the processes in the pool to finish all the tasks
How to put multiple tasks into the thread pool
For + submit multiple tasks in a way
Map (func, iterable) way to submit multiple tasks
Efficiency comparisons through process pool thread pools
The process pool, whether pool or processpoolexecutor (), performs fairly efficiently
Threadpoolexecutor are much less efficient.
So when computing is dense, multiple processes are used.
Thread return value
Submit multiple tasks with a For + Submit method, and use result to get results
fromConcurrent.futuresImportThreadpoolexecutor,processpoolexecutor#thread Async and process Async#Concurrent.futures This module is an asynchronous call mechanism#From multiprocessing Import Pool This module can be either asynchronous or synchronous,deffunc (num): Sum=0 forIinchrange (num): Sum+ = I**2returnsumif __name__=='__main__': P= Threadpoolexecutor (10) Re= [] forNinchRange (100): #P.submit (func, args= (n,))R = P.submit (func, N)#Submit a TaskRe.append (R)#P.shutdown () #等效于进程池中的close +join means to have the parent thread wait for all the processes in the pool to finish all the tasks[Print(I.result ()) forIinchRE]#In the pool process that results, using the Get method, use the result method in Threadpoolexecutor to get results
Submit multiple tasks in map mode, return the Generator object, and use the __next__ () method to get the result
callback function
Queue and Thread replenishment