directory

  • Introduction to Python thread pools
  • The Python thread pool principle
  • ThreadPoolExecutor ¶
  • Python thread pool ThreadPoolExecutor simple to use
  • Five. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Introduction to Python thread pools

In previous articles we have covered a lot about Python threads, such as the thread mutex Lock, the thread Event, and the thread Condition variable. Today we will cover the thread pool ThreadPoolExecutor. The threading module can create threads. ThreadPoolExecutor can create threads. What’s the difference?

As we all know, the use of threads in the program will improve the efficiency of running, although the thread is the smallest unit of the computer, but the creation and use of threads will occupy computer resources and overhead, once created thousands of threads, the computer will crash! A reasonable program is always trying to do the most with the least amount of resources, just as a company owner is always trying to do the most with the least amount of money by hiring the least number of people!

Some asshole is talking behind my back

The Python thread pool principle

Everyone has used the thunderbolt download, when the download 1000 tasks or even more, even if opened VIP at the same time download the number of only 8. Creating 1000 threads at the same time would be a lot of overhead for the calculator at first, and running only 8 threads at a time would require constant creation and destruction, which would be very cumbersome.

Using a thread pool called ThreadPoolExecutor, you can solve this problem by using eight threads, one for each thread, and the rest of the tasks queued. When a thread has finished, the queued tasks can be assigned to that thread. This is called the ThreadPoolExecutor principle!

ThreadPoolExecutor ¶

  • 1. When ThreadPoolExecutor constructs an instance, the max_workers parameter is passed to set the maximum number of concurrent threads in the thread pool.
  • 2. Use the Submit function to submit the task (function name and parameters) to the thread pool, and return the handle to the task (similar to file, drawing). Note that submit does not block, but returns immediately.
  • 3. The done method can be used to determine whether the task is finished by the task handle returned by the Submit function. As shown in the following example, task1 is not completed immediately after the task is submitted due to a delay of 2 seconds, while task1 is completed after a delay of 4 seconds.
  • 4. You can cancel a submitted task by using the Cancel method. If the task is already running in the thread pool, it cannot be canceled. In this example, the thread pool size is set to 2 and the task is already running, so cancellation fails. If you change the size of the thread pool to 1, task1 will be submitted first and Task2 will be queued, which will cancel successfully.
  • 5. Use the result method to get the return value of the task. Note that this method blocks.

Python thread pool ThreadPoolExecutor simple to use

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread pool threadpoolexecutor. py @time :2021/05/05 07:37 @Motto: A long journey without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! Def down_video(times) from concurrent.futures import ThreadPoolExecutor import time # def down_video(times): time.sleep(times) print("down video {}s finished".format(times)) return times executor = ThreadPoolExecutor(max_workers=2) # submit the executed function to the thread pool. Task1 = executor.submit(down_video, (3)) Task2 = executor.submit(down_video, (3)) Print (" task1 completed: ",task1.done()) print(" task1 completed: ",task1.done()) ",task2.cancel()) time.sleep(4) print(" Task1.done () ") # result print(task1.result())" Task 1 Completed: False Cancel Task 2: False Down video 2s finished Video 3s finished Task 1 completed: True 3 ""Copy the code

The thread pool ThreadPoolExecutor goes further than that, and due to space constraints, For details on the blocking and execution order of the thread pool as_completed/map/wait functions and the thread pool, see Python thread Pool ThreadPoolExecutor (2)

Five.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python function declarations and calls
  5. Python variable argument *argc/**kargcs
  6. Python anonymous function lambda
  7. Python return logic determines expressions
  8. Python string/list/tuple/dictionary conversions
  9. Python local and global variables
  10. The Python type function is different from the isinstance function
  11. Python is differs from ==
  12. Python mutable and immutable data types
  13. Shallow and deep copies of Python
  14. Read and write Python files
  15. Python exception Handling
  16. Python module import
  17. Python __name__ == ‘__main__’ explained in detail
  18. Python thread creation and parameter passing
  19. Python thread mutex Lock
  20. Python thread Event Event
  21. The Python thread Condition variable Condition
  22. Python thread Timer Timer
  23. Python thread Semaphore
  24. Python thread Barrier object Barrier
  25. Python thread Queue Queue – FIFO
  26. Python thread queue LifoQueue – LIFO
  27. Python thread PriorityQueue PriorityQueue

Python thread pool ThreadPoolExecutor

This article is published by the blog – Ape Say Programming Ape Say programming!