import time
# threadpool为线程池模块
import threadpool
def test(str):
print str
time.sleep(2)
if __name__ == "__main__":
startTime = time.time()
# 创建线程池,最多创建的线程数为10
pool = threadpool.ThreadPool(10)
# 创建请求(类似于线程创建),test是目标函数,arts_list为目标函数的参数
requests = threadpool.makeRequests(test, args_list=['thread1', 'thread2', 'thread3', 'thread4'])
# 将请求放入线程池中启动
[pool.putRequest(req) for req in requests]
# 等待所有线程执行完毕
pool.wait()
endTime = time.time()
print endTime - startTime
单线程执行该方法的结果
def test(str):
print str
time.sleep(2)
if __name__ == "__main__":
startTime = time.time()
for i in range(4):
test(str(i))
endTime = time.time()
print endTime - startTime