线程池, 固定指定数量的线程来执行任务, 避免重复创建和销毁线程时的资源消耗
Project description
线程池, 固定指定数量的线程来执行任务, 避免重复创建和销毁线程时的资源消耗
注意:
如果任务数量超过线程数量, 超出的任务会等待有正在执行的任务执行完毕后, 在有空闲线程时才会执行
测试代码:
if __name__ == '__main__':
import time
import threading
p = ThreadPool(2)
@p.task()
def fun(a, c):
print(a, '开始', time.strftime(' %H:%M:%S', time.localtime()))
for i in range(c):
time.sleep(0.01)
print(a, ' 结束', time.strftime(' %H:%M:%S', time.localtime()))
fun('aa', 100)
fun('bb', 100)
fun('cc', 100)
fun('dd', 100)
p.close()
p.join()
print('--end--')
注意:
如果任务数量超过线程数量, 超出的任务会等待有正在执行的任务执行完毕后, 在有空闲线程时才会执行
测试代码:
if __name__ == '__main__':
import time
import threading
p = ThreadPool(2)
@p.task()
def fun(a, c):
print(a, '开始', time.strftime(' %H:%M:%S', time.localtime()))
for i in range(c):
time.sleep(0.01)
print(a, ' 结束', time.strftime(' %H:%M:%S', time.localtime()))
fun('aa', 100)
fun('bb', 100)
fun('cc', 100)
fun('dd', 100)
p.close()
p.join()
print('--end--')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zthreadpool-1.0.1.tar.gz
(3.4 kB
view details)
Built Distribution
File details
Details for the file zthreadpool-1.0.1.tar.gz
.
File metadata
- Download URL: zthreadpool-1.0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
86e2df8df76a8269e04235adf3db05e5f6bfbadfe8569408143b3d830776ccc3
|
|
MD5 |
46a5ffff875f570ee0b4efb587a12eaa
|
|
BLAKE2b-256 |
20827b99a03fe31595fb276e1eaf7cfc3ad5a2bf1e81026fc40d435ccf10bdf7
|
File details
Details for the file zthreadpool-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: zthreadpool-1.0.1-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
8fecc5d6a4a5e5ab8d5ab161796a8c06f9faa3dfa42f1e82bc7cbdf0a297a5cf
|
|
MD5 |
b12ea1f672099a58141c02f53bd96740
|
|
BLAKE2b-256 |
3606116ae0e1c77b3988ef903506786f4cb3a338802f87d291d666f57193e94c
|