test.py 779 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import time
  2. import threading
  3. from concurrent.futures import ThreadPoolExecutor
  4. # method对应于:自行编写的方法,可传参(可传多个参数,形如pool.submit(self.method, a,b))
  5. def method1():
  6. print("我是方法1")
  7. return 1
  8. def method2():
  9. print("我是方法2")
  10. return 2
  11. def method3():
  12. print("我是方法3")
  13. return 3
  14. def method4():
  15. print("我是方法4")
  16. return 4
  17. def method5():
  18. print("我是方法5")
  19. return 5
  20. def fun():
  21. start = time.time()
  22. # 创建包含5个线程的线程池
  23. pool = ThreadPoolExecutor(max_workers=5)
  24. future1 = pool.submit(method1)
  25. future2 = pool.submit(method2)
  26. future3 = pool.submit(method3)
  27. future4 = pool.submit(method4)
  28. future5 = pool.submit(method5)