Python thread pool ThreadPool using the article

Source: Internet
Author: User
This article is mainly for you to introduce the Python thread pool ThreadPool use method, with a certain reference value, interested in small partners can refer to

Recently in a video equipment management project, equipment including (cameras, DVR,NVR, etc.), including equipment information completion, device status push, device flow address push, etc., if the number of devices imported at the same time, if the use of single-threaded device detection, then because of the number of devices, will bring greater delay, So consider multithreading to handle this issue.

You can implement the thread pool yourself using the Python language, or you can use a third-party package threadpool thread pool package, which focuses on the use of ThreadPool and the specific implementation within it.

1. Installation

Using the installation:

Pip Installthreadpool

2. Use

(1) Introduction of ThreadPool Module
(2) Defining thread functions
(3) Create thread pool ThreadPool. ThreadPool ()
(4) Create a task that requires a thread pool to process threadpool.makerequests ()
(5) Put multiple tasks created into the thread pool, threadpool.putrequest
(6) Wait until all tasks are processed theadpool.pool ()

Import ThreadPool def threadfun (arg1,arg2):  pass def Main ():  device_list=[object1,object2,object3 ... OBJECTN] #需要处理的设备个数  task_pool=threadpool. ThreadPool (8) #8是线程池中线程的个数  request_list=[] #存放任务列表  #首先构造任务列表 for  device in Device_list:  request_ List.append (threadpool.makerequests (threadfun,[(device, {}))))  #将每个任务放到线程池中, wait for thread pool threads to read the task separately, and then process it. Using the map function, you can get to know what you don't know.  map (task_pool.putrequest,request_list)  #等待所有任务处理完成, then return, if not finished, will always block  task_pool.poll () if __name__= = "__MAIN__":  Main ()

The above is a specific thread pool usage process
ThreadPool specific definitions are as follows:

Class ThreadPool: ""  A thread pool, distributing work requests and collecting results.   See the module docstring for more information.  "" " def __init__ (self, num_workers, q_size=0, Resq_size=0, poll_timeout=5):  pass  def createworkers (self, num_ Workers, poll_timeout=5):  pass  def dismissworkers (self, Num_workers, do_join=false):  pass  def Joinalldismissedworkers (self):  pass  def putrequest (self, request, Block=true, Timeout=none):  Pass  def poll (self, block=false):  pass  def wait (self):  Pass

The next section details the entire process above and each function: Python thread pool ThreadPool (Implementation chapter)

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.