This article mainly introduces the use of python thread pool threadpool in detail. It has certain reference value. Interested friends can refer to
I am currently working on a video device management project. , devices include (camera, DVR, NVR, etc.), including device information completion, device status push, device stream address push, etc. If a large number of devices are imported at the same time, if a single thread is used for device detection, then due to the large number of devices If there are too many, it will bring a large delay, so consider multi-threading to handle this problem.
You can use python language to implement the thread pool yourself, or you can use the third-party package threadpool thread pool package. This topic mainly introduces the use of threadpool and its specific implementation.
1. Installation
Use installation:
pip installthreadpool
2 , use
(1) Introduce the threadpool module
(2) Define the thread function
(3) Create a thread pool threadpool.ThreadPool()
(4) Create a thread pool that is required The task to be processed is threadpool.makeRequests()
(5) Put the multiple created tasks 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, ), {})])) #将每个任务放到线程池中,等待线程池中线程各自读取任务,然后进行处理,使用了map函数,不了解的可以去了解一下。 map(task_pool.putRequest,request_list) #等待所有任务处理完成,则返回,如果没有处理完,则一直阻塞 task_pool.poll() if __name__=="__main__": main()
The above is a specific thread pool usage process
The specific definition of threadpool is 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 will introduce the above entire process and each function in detail: python thread pool threadpool (implementation)
Related recommendations:
Implementation of python thread pool threadpool
The above is the detailed content of python thread pool threadpool usage articles. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
