当前位置:   article > 正文

python进程池multiprocessing.Pool和线程池multiprocessing.dummy.Pool实例

python from multiprocessing.pool multiprocessing.dummy import pool

进程池:

进程池的使用有四种方式:apply_async、apply、map_async、map。其中apply_async和map_async是异步的,也就是启动进程函数之后会继续执行后续的代码不用等待进程函数返回。apply_async和map_async方式提供了一写获取进程函数状态的函数: ready()successful()get()。
PS: join()语句要放在 close()语句后面。
 
实例代码如下:
  1. # -*- coding: utf-8 -*-
  2. import multiprocessing
  3. import time
  4. def func(msg):
  5. print('msg: ', msg)
  6. time.sleep(1)
  7. print('********')
  8. return 'func_return: %s' % msg
  9. if __name__ == '__main__':
  10. # apply_async
  11. print('\n--------apply_async------------')
  12. pool = multiprocessing.Pool(processes=4)
  13. results = []
  14. for i in range(10):
  15. msg = 'hello world %d' % i
  16. result = pool.apply_async(func, (msg, ))
  17. results.append(result)
  18. print('apply_async: 不堵塞')
  19. for i in results:
  20. i.wait() # 等待进程函数执行完毕
  21. for i in results:
  22. if i.ready(): # 进程函数是否已经启动了
  23. if i.successful(): # 进程函数是否执行成功
  24. print(i.get()) # 进程函数返回值
  25. # apply
  26. print('\n--------apply------------')
  27. pool = multiprocessing.Pool(processes=4)
  28. results = []
  29. for i in range(10):
  30. msg = 'hello world %d' % i
  31. result = pool.apply(func, (msg,))
  32. results.append(result)
  33. print('apply: 堵塞') # 执行完func才执行该句
  34. pool.close()
  35. pool.join() # join语句要放在close之后
  36. print(results)
  37. # map
  38. print('\n--------map------------')
  39. args = [1, 2, 4, 5, 7, 8]
  40. pool = multiprocessing.Pool(processes=5)
  41. return_data = pool.map(func, args)
  42. print('堵塞') # 执行完func才执行该句
  43. pool.close()
  44. pool.join() # join语句要放在close之后
  45. print(return_data)
  46. # map_async
  47. print('\n--------map_async------------')
  48. pool = multiprocessing.Pool(processes=5)
  49. result = pool.map_async(func, args)
  50. print('ready: ', result.ready())
  51. print('不堵塞')
  52. result.wait() # 等待所有进程函数执行完毕
  53. if result.ready(): # 进程函数是否已经启动了
  54. if result.successful(): # 进程函数是否执行成功
  55. print(result.get()) # 进程函数返回值

  

线程池

线程池的使用方式和进程池类似。
 
实例代码如下:
  1. # -*- coding: utf-8 -*-
  2. from multiprocessing.dummy import Pool as ThreadPool
  3. import time
  4. def fun(msg):
  5. print('msg: ', msg)
  6. time.sleep(1)
  7. print('********')
  8. return 'fun_return %s' % msg
  9. # map_async
  10. print('\n------map_async-------')
  11. arg = [1, 2, 10, 11, 18]
  12. async_pool = ThreadPool(processes=4)
  13. result = async_pool.map_async(fun, arg)
  14. print(result.ready()) # 线程函数是否已经启动了
  15. print('map_async: 不堵塞')
  16. result.wait() # 等待所有线程函数执行完毕
  17. print('after wait')
  18. if result.ready(): # 线程函数是否已经启动了
  19. if result.successful(): # 线程函数是否执行成功
  20. print(result.get()) # 线程函数返回值
  21. # map
  22. print('\n------map-------')
  23. arg = [3, 5, 11, 19, 12]
  24. pool = ThreadPool(processes=3)
  25. return_list = pool.map(fun, arg)
  26. print('map: 堵塞')
  27. pool.close()
  28. pool.join()
  29. print(return_list)
  30. # apply_async
  31. print('\n------apply_async-------')
  32. async_pool = ThreadPool(processes=4)
  33. results =[]
  34. for i in range(5):
  35. msg = 'msg: %d' % i
  36. result = async_pool.apply_async(fun, (msg, ))
  37. results.append(result)
  38. print('apply_async: 不堵塞')
  39. # async_pool.close()
  40. # async_pool.join()
  41. for i in results:
  42. i.wait() # 等待线程函数执行完毕
  43. for i in results:
  44. if i.ready(): # 线程函数是否已经启动了
  45. if i.successful(): # 线程函数是否执行成功
  46. print(i.get()) # 线程函数返回值
  47. # apply
  48. print('\n------apply-------')
  49. pool = ThreadPool(processes=4)
  50. results =[]
  51. for i in range(5):
  52. msg = 'msg: %d' % i
  53. result = pool.apply(fun, (msg, ))
  54. results.append(result)
  55. print('apply: 堵塞')
  56. print(results)

  

计算多的用多进程

io多的用多线程

转载于:https://www.cnblogs.com/dylan9/p/9207366.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/74902
推荐阅读
相关标签
  

闽ICP备14008679号