1、

https://blog.csdn.net/wonengguwozai/article/details/80325745

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

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

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

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

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

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

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

使用 join()

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

子进程的开始时间

  1.  
    # -*- coding:utf-8 -*-
  2.  
    from multiprocessing import Process
  3.  
    import os
  4.  
    import time
  5.  
     
  6.  
     
  7.  
    def run_proc(name):
  8.  
    print(time.time())
  9.  
     
  10.  
    time.sleep(10)
  11.  
    print('Run child process %s (%s)...' % (name, os.getpid()))
  12.  
     
  13.  
     
  14.  
    def hello_world():
  15.  
    print(time.time())
  16.  
     
  17.  
    # time.sleep(5)
  18.  
    time.sleep(20)
  19.  
    print('hello world!')
  20.  
    print('Run child process (%s)...' % (os.getpid()))
  21.  
     
  22.  
     
  23.  
    if __name__ == '__main__':
  24.  
    print ('Parent process %s.' % os.getpid())
  25.  
    p1 = Process(target=run_proc, args=('test',))
  26.  
    p2 = Process(target=hello_world)
  27.  
    print 'Process will start.'
  28.  
    p1.start()
  29.  
    p2.start()
  30.  
    p1.join()
  31.  
    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.  
     
  10.  
    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.  
     
  6.  
     
  7.  
    def run_proc(name):
  8.  
    print(time.time())
  9.  
     
  10.  
    time.sleep(10)
  11.  
    print('Run child process %s (%s)...' % (name, os.getpid()))
  12.  
     
  13.  
     
  14.  
    def hello_world():
  15.  
    print(time.time())
  16.  
     
  17.  
    # time.sleep(5)
  18.  
    time.sleep(20)
  19.  
    print('hello world!')
  20.  
    print('Run child process (%s)...' % (os.getpid()))
  21.  
     
  22.  
     
  23.  
    if __name__ == '__main__':
  24.  
    print ('Parent process %s.' % os.getpid())
  25.  
    p1 = Process(target=run_proc, args=('test',))
  26.  
    p2 = Process(target=hello_world)
  27.  
    print 'Process will start.'
  28.  
    # p1.start()
  29.  
    # p2.start()
  30.  
    # p1.join()
  31.  
    p_list = (p1, p2)
  32.  
    map(Process.start, p_list)
  33.  
    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.  
     
  10.  
    Process finished with exit code 0

2.

https://blog.csdn.net/yagamil/article/details/51284851

threads=[]
f=[fast,slow]
l=len(f)
for i in range(l):
t=MyThread(f[i],(),str(i))
threads.append(t)

for i in range(l):
threads[i].start()

for i in range(l):
#pass
threads[i].join()
print threads[i].getName()
print "Done on main"
exit()

join的主要功能是阻塞线程,也就是使用了join后,后面的语句需要等当前进程完成之后才能 执行。

那么看看下面的例子

class MyThread(threading.Thread):
def __init__(self,fun,arg,name=""):
threading.Thread.__init__(self)
self.fun=fun
self.arg=arg
self.name=name
#self.result

def run(self):
self.result=apply(self.fun,self.arg)

def getName(self):
return self.name

def getResult(self):
return self.result

上面是一个多线程的类。 待会用来调用的。
下面是main函数

def fast():
print "in fast"
sleep(15)
print "done in fast"

def slow():
print "in slow"
sleep(10)
print "done in slow"

threads=[]
f=[fast,slow]
l=len(f)
for i in range(l):
t=MyThread(f[i],(),str(i))
threads.append(t)

for i in range(l):
threads[i].start()

for i in range(l):
#pass
threads[i].join()
print threads[i].getName()
print "Done on main"
exit()

运行之后
输出的结果是

in fast
in slow
done in slow
done in fast
0
1
Done on main

看到了吗?

运行到thread[i].join() 这一行, 下一句没有马上被执行,需要等到done in fast 完成之后才执行, 而为什么done in slow却提前被执行了呢?

因为上面执行了threads[i].start() 后 两个线程已经同时开启了,但是由于在slow线程里只sleep了10秒,而fast线程sleep了15秒,所以会先打印done in slow。

print threads[i].getName()

而上面这句则需要等线程1执行之后才会执行,

等执行完了上面那句之后,才会执行线程2的 join(), 由于此时的线程已经早已执行完毕,所以这句也没有起到阻塞作用,故会马上执行 下面只一句

print threads[i].getName()
---------------------
作者:重复的生活
来源:CSDN
原文:https://blog.csdn.net/yagamil/article/details/51284851
版权声明:本文为博主原创文章,转载请附上博文链接!

转 Python 多进程multiprocessing.Process之satrt()和join()的更多相关文章

  1. python多进程——multiprocessing.Process

    简介 multiprocessing是一个使用类似于threading模块的API支持生成进程的包.该multiprocessing软件包提供本地和远程并发.因此,该multiprocessing模块 ...

  2. Python 多进程 multiprocessing.Pool类详解

    Python 多进程 multiprocessing.Pool类详解 https://blog.csdn.net/SeeTheWorld518/article/details/49639651

  3. Python多进程multiprocessing使用示例

    mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...

  4. Python 多进程multiprocessing

    一.python多线程其实在底层来说只是单线程,因此python多线程也称为假线程,之所以用多线程的意义是因为线程不停的切换这样比串行还是要快很多.python多线程中只要涉及到io或者sleep就会 ...

  5. python ---多进程 Multiprocessing

    和 threading 的比较 多进程 Multiprocessing 和多线程 threading 类似, 他们都是在 python 中用来并行运算的. 不过既然有了 threading, 为什么 ...

  6. python多进程-----multiprocessing包

    multiprocessing并非是python的一个模块,而是python中多进程管理的一个包,在学习的时候可以与threading这个模块作类比,正如我们在上一篇转载的文章中所提,python的多 ...

  7. python多进程multiprocessing Pool相关问题

    python多进程想必大部分人都用到过,可以充分利用多核CPU让代码效率更高效. 我们看看multiprocessing.pool.Pool.map的官方用法 map(func, iterable[, ...

  8. 操作系统OS,Python - 多进程(multiprocessing)、多线程(multithreading)

    多进程(multiprocessing) 参考: https://docs.python.org/3.6/library/multiprocessing.html 1. 多进程概念 multiproc ...

  9. python多进程(multiprocessing)

    最近有个小课题,需要用到双进程,翻了些资料,还算圆满完成任务.记录一下~ 1.简单地双进程启动 同时的调用print1()和print2()两个打印函数,代码如下: #/usr/bin/python ...

随机推荐

  1. RPM验证与数字签名(Verify/Signature)

    RPM验证与数字签名(Verify/Signature) 摘自:https://blog.csdn.net/rhel_admin/article/details/32382391 2014年06月19 ...

  2. SDUT 3362 数据结构实验之图论六:村村通公路

    数据结构实验之图论六:村村通公路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 当前农村公 ...

  3. Sublime Text3注册码供研究使用

    文章介绍 看到文章时候感觉不错,一直用的Notepad++.文章属于转载,文末有文章来源,转载注明出处. 一. Sublime 下载地址: Sublime 3: http://www.sublimet ...

  4. java打成jar包后,class,getResource()出现null指针异常

    1.SqlHelper.java有包路径没?如果有,是不是类似于com.db.jdbc? 不管怎么办,你SqlHelper.class.getResourceAsStream("/mysql ...

  5. libsvm使用简介

    libsvm是support vector machine的一种开源实现,采用了smo算法.源代码编写有独到之处,值得一睹. 常用结构 svm_node结构 定义了构成输入特征向量的元素,index为 ...

  6. 从零开始搭建.NET Core 2.0 API(学习笔记一)

    从零开始搭建.NET Core 2.0 API(学习笔记一) 一. VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2. ...

  7. UWP&WP8.1 基础控件——Grid

    Grid是一个面板控件  Grid是UWP和WPF,WP8.1中最重要的一个控件,相当相当重要. 他是一个面板控件,是用来添加其他控件   但是呢 用法确实简单的很. 大概就这个样子. 你用工具箱拖, ...

  8. mac的idea不能编辑问题

    在安装的时候,因为在选择插件的时候,把IDEAVim这个玩意儿选上了.所以,编辑模式就跟命令行里面的Vim一样.输入时,需要先输入i, 进入insert模式下,然后才可以编辑.彻底解决办法就是进入Pr ...

  9. Python中的可迭代对象

      Python中的可迭代对象有:列表.元组.字典.字符串:常结合for循环使用: 判断一个对象是不是可迭代对象: from collections import Iterable isinstanc ...

  10. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...