程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed introduction to Python thread pool ThreadPoolExecutor

編輯:Python

1、 How to use thread pool ?

    Actual case :

         We previously implemented a multithreading web Video surveillance server , We need to limit the number of requested connections , To prevent malicious users from initiating a large number of connections and causing the server to create a large number of threads , Finally, it is paralyzed due to the exhaustion of resources .

         You can use the thread pool , Instead of creating threads for each request .

    Solution :

        python3 Implementation of medium-sized wire pool , Use standard library concurrent.futures Under the ThreadPoolExecutor, Object's submit and map Method can be used to start threads in the thread pool to execute tasks .

    Introduction to thread pool :

         Thread pool is to create a fixed number of threads in advance and put them in the pool , When you need to use it, take a thread from the pool and let it execute tasks , After execution, it will be returned to the thread pool so that subsequent tasks can continue to use threads .

2、 Code demonstration

         Simple use of multithreaded pools

from concurrent.futures import ThreadPoolExecutor
# establish Executor object , Specify the number of threads in the thread pool
executor = ThreadPoolExecutor(3)
# Use threads in the thread pool to execute tasks
# Define task functions a Of b Power
def f(a, b):
print('f', a, b)
return a ** b
# Call the thread in the thread pool to execute the function
future = executor.submit(f, 2, 3)
# Use a thread in the thread pool to run this function , After this function runs
# This thread will be returned to the thread pool
# Use result Get the running result of the function
print(future.result())
# If the running event of a function is long , Calling result He had not finished the execution by the time of ,
# This result Will be blocked here , Until this function runs
# and python Built in map The method is similar to , It's just that it calls on multiple threads at the same time f
# In the 1 Thread Computing 2 Of 4 Power , The first 2 Thread Computing 3 Of 5 Power , The first 3 Thread Computing 5 Of 6 Power
executor.map(f, [2, 3, 5], [4, 5, 6])
'''
Assume that all threads in the current thread pool are busy , The function has been running and has not returned ,
When we submit another task , Will appear pending,
Waiting for an idle thread in the thread pool to run it .
'''
import time
def f2(a, b):
print('f2', a, b)
time.sleep(10)
# wait for 10 Seconds and then back
return a ** b
# Run the following statement to , You can see the first print 'f2 2 4'、'f2 3 5'、'f2 5 6'
# After a while 'f2 6 7' and 'f2 7 8' Print out , He didn't get the right to run until he quit
executor.map(f2, [2, 3, 5, 6, 7], [4, 5, 6, 7, 8])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved