#coding=utf-8

import time
import threadpool

def wait_time(n):
    print('%d\n' % n)
    time.sleep(2)
#在线程池中开启3个线程,一般小于cpu核数
pool = threadpool.ThreadPool(3)
tasks = threadpool.makeRequests(wait_time, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(len(tasks))
[pool.putRequest(task) for task in tasks]
pool.wait()


'''
#结果就会3个一组跑起来
>>> 
10
1
2
3



4
5
6



7
8
9



10

>>> 
'''


#下面这段也可以,把参数提取出来

ns=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
pool1=threadpool.ThreadPool(3)
tasks1=threadpool.makeRequests(long_op,ns)
[pool1.putRequest(t)for t in tasks1]
pool1.wait()











  

相关文章:

  • 2021-11-08
  • 2022-02-02
  • 2021-11-13
  • 2021-08-27
  • 2022-12-23
  • 2022-02-03
猜你喜欢
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2021-09-24
  • 2021-09-17
相关资源
相似解决方案