import time
import threading
import threadpool
def test(a, b):
print threading.current_thread(), a, b
time.sleep(1)
# 参数格式
data =[(['1','2'], None), (['3','4'], None)]
start_time = time.time()
# 创建线程池
pool = threadpool.ThreadPool(10)
# 执行线程
requests=threadpool.makeRequests(test, data)
[pool.putRequest(req) for req in requests]
pool.wait()
print '%d second'% (time.time()-start_time)
本文介绍了一个使用Python的threadpool库实现线程池的具体案例。通过定义一个名为test的函数来展示如何在线程中打印传入的参数,并利用threadpool创建和管理线程,有效地控制了并发任务的数量。
1469

被折叠的 条评论
为什么被折叠?



