话不多说直接上代码
"""
线程池模块
"""
def multiThread(functionName, taskList, threadNum):
from multiprocessing.pool import ThreadPool
pool = ThreadPool(threadNum)
pm = pool.starmap_async(functionName, taskList)
pool.close()
pool.join()
return pm
def hello(num,nstr):
print(num,nstr)
return num,nstr
if __name__ == '__main__':
pool = multiThread(hello,[(row, 'hello') for row in range(10)],10) # 运行带参数传入'hello'
print(pool.get())
output
01 hello234 hello
hello
5 hello
6
789hello
hello hellohello
hello
hello
[(0, 'hello'), (1, 'hello'), (2, 'hello'), (3, 'hello'), (4, 'hello'), (5, 'hello'), (6, 'hello'), (7, 'hello'), (8, 'hello'), (9, 'hello')]
进程池的异步方法类似
异步-简单又快乐