本文整理汇总了Python中multiprocessing.pool.ThreadPool.add_task方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.add_task方法的具体用法?Python ThreadPool.add_task怎么用?Python ThreadPool.add_task使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.pool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.add_task方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from multiprocessing.pool import ThreadPool [as 别名]
# 或者: from multiprocessing.pool.ThreadPool import add_task [as 别名]
delays = [randrange(1, 10) for i in range(100)]
from time import sleep
def wait_delay(d):
print('sleeping for (%d)sec' % d)
sleep(d)
# 1) Init a Thread pool with the desired number of threads
pool = ThreadPool(20)
for i, d in enumerate(delays):
# print the percentage of tasks placed in the queue
print('%.2f%c' % ((float(i)/float(len(delays)))*100.0,'%'))
# 2) Add the task to the queue
pool.add_task(wait_delay, d)
# 3) Wait for completion
pool.wait_completion()
class asynchronous(object):
def __init__(self, func):
self.func = func
def threaded(*args, **kwargs):
self.queue.put(self.func(*args, **kwargs))
self.threaded = threaded
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)