当前位置:   article > 正文

python threadPool 与 multiprocessing.Pool

python multiprocessing.pool threadpool

测试进程沲和线程沲

    为了和官方文档保持一致 ,这里把线程沲叫做 ThreadPool ,进程沲叫 multiprocessing.Pool。

线程沲(ThreadPool)

    通过如下导入实现

  1. from multiprocessing.pool import ThreadPool 
  2. from multiprocessing.dummy import Pool as ThreadPool

进程沲(multiprocessing.Pool)

    通过如下导入实现

from multiprocessing import Pool


观察方法

    在 ThreadPool 版本和 multiprocessing.Pool 版本的脚本执行期间,通过如下命令观察 python 进程数。   

    可以观察到,当 processes=8 时,ThreadPool 版本只有 1 个进程,而 multiprocessing.Pool 版本有 9 个进程。

  1. while true; do ps -ef|grep python|grep -v grep|grep -v denyhost; sleep 1;echo -e "--------\n"; done
  2. # pool = ThreadPool(processes=8)
  3. # ThreadPool 输出结果
  4. --------
  5. root     535648 525834 67 22:20 pts/1    00:00:07 python threading_ssh.py
  6. --------
  7. root     535648 525834 61 22:20 pts/1    00:00:07 python threading_ssh.py
  8. --------
  9. # pool = Pool(processes=8)
  10. # multiprocessing.Pool 输出结果
  11. --------
  12. root     536188 525834  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  13. root     536191 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  14. root     536192 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  15. root     536193 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  16. root     536194 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  17. root     536195 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  18. root     536196 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  19. root     536197 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  20. root     536198 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  21. --------
  22. root     536188 525834  6 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  23. root     536191 536188  8 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  24. root     536192 536188  3 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  25. root     536193 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  26. root     536194 536188  0 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  27. root     536195 536188  9 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  28. root     536196 536188  9 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  29. root     536197 536188  4 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  30. root     536198 536188  9 22:21 pts/1    00:00:00 python multpRemoteCmd.py
  31. --------

multiprocessing.dummy

    如下是 python 官方文档对 multiprocessing.dummy 的解释,multiprocessing.dummy 复制了 multiprocessing 的 API ,但是底层是通过封装 threading 模块来实现的。

    multiprocessing.dummy replicates the API of multiprocessing but is no more than a wrapper around the threading module.


转载于:https://my.oschina.net/payun/blog/509804

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

闽ICP备14008679号