Queue and Thread replenishment

Source: Internet
Author: User
Tags first string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.