一个简单的线程池库
pali的Python项目详细描述
帕利
pali是一个简单的python线程池库。它与python3和python2兼容。
pali可用于:
- 正在创建数据管道。
- 在消息代理中处理请求(在TCP/IP层的顶部)。
- 模拟任何压力测试系统。
- API测试系统。
用法
pali使用起来很简单。pali的工作池处理在pali.task.Task
中具有定义良好的接口的任务。
新任务可以按如下方式从中扩展。_run
方法对于定义为
线程池在内部查找并调用此函数作为任务的开始。
>>> from pali import worker, task
>>> class MyTask(task.Task):
... def __init__(self, ident):
... self.task_id = ident
... self.done = False
...
... def _run(self):
... self.done = True
线程池本身既可以作为上下文管理器工作,也可以手动调用。下面的例子说明了它的用途 作为上下文管理器。
# create 10 tasks
>>> tasks = [MyTask(i) for i in range(10)]
# Start a Thread Pool and push tasks on it.
# Tasks are processed as and when they come.
>>> with worker.ThreadPool(3) as tpool:
... _ = [tpool.append_task(t) for t in tasks]
# Check the status of Tasks
>>> status = [t.done for t in tasks]
>>> status
[True, True, True, True, True, True, True, True, True, True]
更多的例子可以在https://github.com/gitvipin/Pali/tree/master/tests找到,它们反映了操作系统使用线程池的不同方式。
要求
python 2.7+和python 3.4+
分享和享受!