test_pool.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ = '线程池测试'
  5. @Time : 2020/9/27 13:57
  6. @Author : zhengwangeng
  7. @Software: PyCharm
  8. # code is far away from bugs with the god animal protecting
  9. I love animals. They taste delicious.
  10. ┏┓ ┏┓
  11. ┏┛┻━━━┛┻┓
  12. ┃ ☃ ┃
  13. ┃ ┳┛ ┗┳ ┃
  14. ┃ ┻ ┃
  15. ┗━┓ ┏━┛
  16. ┃ ┗━━━┓
  17. ┃ 神兽保佑 ┣┓
  18. ┃ 永无BUG! ┏┛
  19. ┗┓┓┏━┳┓┏┛
  20. ┃┫┫ ┃┫┫
  21. ┗┻┛ ┗┻┛
  22. """
  23. import os
  24. import random
  25. import time
  26. from concurrent.futures import ProcessPoolExecutor
  27. def task(n):
  28. print('%s is runing' % os.getpid())
  29. time.sleep(random.randint(1, 3))
  30. return n
  31. if __name__ == '__main__':
  32. print((100 - 1) // 100 + 1)
  33. print((101 - 1) // 100 + 1)
  34. for i in range((6 - 1) // 100 + 1):
  35. print(i, ' == i')
  36. for j in range((101 - 1) // 100 + 1):
  37. print(j, ' == j')
  38. executor = ProcessPoolExecutor(max_workers=3)
  39. futures = []
  40. for i in range(11):
  41. future = executor.submit(task, i)
  42. futures.append(future)
  43. executor.shutdown(True)
  44. print('+++>')
  45. for future in futures:
  46. print(future.result())