当前位置:   article > 正文

Python 多进程multiprocessing.Process之satrt()和join()_python多进程multiprocessing

python多进程multiprocessing

今天项目中涉及到了使用多进程处理数据,在廖雪峰的python教程上学习了一下多进程相关,其中涉及到了start和join函数,解释的不是很清晰,在网上找了博客,敲了下博客提供的代码,瞬间理解了。感谢原文:https://blog.csdn.net/HeatDeath/article/details/72842899

note:一个经典、全面介绍python多进程的博文:链接

由于Python是跨平台的,自然也应该提供一个跨平台的多进程支持。

multiprocessing模块就是跨平台版本的多进程模块。

multiprocessing模块提供了一个Process类来代表一个进程对象。

创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例

start()方法启动,这样创建进程比fork()还要简单。

join()方法可以等待子进程结束后再继续往下运行(更准确地说,在当前位置阻塞主进程,带执行join()的进程结束后再继续执行主进程),通常用于进程间的同步。(进一步地解释,哪个子进程调用了join方法,主进程就要等该子进程执行完后才能继续向下执行,具体可见下边的分析图)

使用 join()

  1. # -*- coding:utf-8 -*-
  2. from multiprocessing import Process
  3. import os
  4. import time
  5. def run_proc(name):
  6. time.sleep(10)
  7. print('Run child process %s (%s)...' % (name, os.getpid()))
  8. def hello_world():
  9. # time.sleep(5)
  10. time.sleep(20)
  11. print('hello world!')
  12. print('Run child process (%s)...' % (os.getpid()))
  13. if __name__ == '__main__':
  14. print ('Parent process %s.' % os.getpid())
  15. p1 = Process(target=run_proc, args=('test',))
  16. p2 = Process(target=hello_world)
  17. print 'Process will start.'
  18. p1.start()
  19. p2.start()
  20. p1.join()
  21. print('Process end.')

输出:

  1. Parent process 11860.
  2. Process will start.
  3. Run child process test (11232)...
  4. Process end.
  5. hello world!
  6. Run child process (2288)...
  7. Process finished with exit code 0
  • 这里写图片描述

子进程的开始时间

  1. # -*- coding:utf-8 -*-
  2. from multiprocessing import Process
  3. import os
  4. import time
  5. def run_proc(name):
  6. print(time.time())
  7. time.sleep(10)
  8. print('Run child process %s (%s)...' % (name, os.getpid()))
  9. def hello_world():
  10. print(time.time())
  11. # time.sleep(5)
  12. time.sleep(20)
  13. print('hello world!')
  14. print('Run child process (%s)...' % (os.getpid()))
  15. if __name__ == '__main__':
  16. print ('Parent process %s.' % os.getpid())
  17. p1 = Process(target=run_proc, args=('test',))
  18. p2 = Process(target=hello_world)
  19. print 'Process will start.'
  20. p1.start()
  21. p2.start()
  22. p1.join()
  23. print('Process end.')
  •  

输出:

  1. Parent process 7220.
  2. Process will start.
  3. 1496374096.56
  4. 1496374096.56
  5. Run child process test (2196)...
  6. Process end.
  7. hello world!
  8. Run child process (832)...
  9. Process finished with exit code 0
  •  

可以认为 子进程 p1 与 子进程 p2 同时开始


去掉 join(),使用 map

  1. # -*- coding:utf-8 -*-
  2. from multiprocessing import Process
  3. import os
  4. import time
  5. def run_proc(name):
  6. print(time.time())
  7. time.sleep(10)
  8. print('Run child process %s (%s)...' % (name, os.getpid()))
  9. def hello_world():
  10. print(time.time())
  11. # time.sleep(5)
  12. time.sleep(20)
  13. print('hello world!')
  14. print('Run child process (%s)...' % (os.getpid()))
  15. if __name__ == '__main__':
  16. print ('Parent process %s.' % os.getpid())
  17. p1 = Process(target=run_proc, args=('test',))
  18. p2 = Process(target=hello_world)
  19. print 'Process will start.'
  20. # p1.start()
  21. # p2.start()
  22. # p1.join()
  23. p_list = (p1, p2)
  24. map(Process.start, p_list)
  25. print('Process end.')
  •  

输出:

  1. Parent process 8580.
  2. Process will start.
  3. Process end.
  4. 1496374397.24
  5. 1496374397.24
  6. Run child process test (7148)...
  7. hello world!
  8. Run child process (8348)...
  9. Process finished with exit code 0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/860677?site
推荐阅读
相关标签
  

闽ICP备14008679号