如何随后运行两个并行进程?

时间:2014-06-13 00:23:46

标签: python multiprocessing

我想确保以下两个并行进程一个接一个地实现。特别是,我希望首先实现十个f函数,在完成该部分之后,实现十个g函数。有谁知道我应该如何修改我的代码?

from multiprocessing import Process
import time
import random

wait_low = 0.1
wait_high = 15

def f(i):
    time.sleep(random.uniform(wait_low, wait_high))
    print 'hello'+str(i)

def g(i):
    time.sleep(random.uniform(wait_low, wait_high))
    print 'hey'+str(i)


if __name__ == '__main__':
    for j in range(10):
        p = Process(target=f, args=(j,))
        p.start()
    p.join()

    print "switch"

    # comment
    for j in range(10):
        q = Process(target=g, args=(j,))
        q.start()
    q.join()

    time.sleep(5)

    print "I'm done"

我得到的结果是:

hello2
hello0
hello1
hello5
hello6
hello8
hello3
hello7
hello9
switch
hey6
hey3
hello4
hey9
hey8
I'm done
hey2
hey0
hey1
hey5
hey7
hey4

非常感谢!

3 个答案:

答案 0 :(得分:3)

所有fg都需要加入。

if __name__ == '__main__':
    fs = []
    for j in range(10):
        p = Process(target=f, args=(j,))
        p.start()
        fs.append(p)

    for f in fs:
        f.join()

    print "switch"

    # comment
    gs = []
    for j in range(10):
        q = Process(target=g, args=(j,))
        q.start()
        gs.append(q)

    for g in gs:
        g.join()

    print "I'm done"

输出:

hello2
hello8
hello5
hello6
hello9
hello1
hello4
hello3
hello7
hello0
switch
hey0
hey7
hey2
hey8
hey4
hey3
hey1
hey9
hey5
hey6
I'm done

答案 1 :(得分:2)

您的问题出现在您的代码中,您只是加入了在循环中生成的最后一个进程,您可以在之前的进程完成之前继续,这会导致输出的交错。

您可以使用进程池:

from multiprocessing.pool import Pool
import random
import time

wait_low = 0
wait_high=1
def f(i):
    time.sleep(random.uniform(wait_low, wait_high))
    return 'hello'+str(i)

def g(i):
    time.sleep(random.uniform(wait_low, wait_high))
    return 'hey'+str(i)


pool = Pool()
for output in pool.imap_unordered(f, range(10)):
    print output
for output in pool.imap_unordered(g, range(10)):
    print output

答案 2 :(得分:0)

使用阻止map功能而不是自己完成细节工作。您可以使用内置的multiprocessing执行以下操作,但由于我很懒,我只是从解释器执行此操作(这样做需要multiprocessing的分支pathos

>>> from pathos.multiprocessing import ProcessingPool as Pool
>>> import time
>>> import random
>>> 
>>> wait_low = 0.1
>>> wait_high = 15
>>> 
>>> def f(i):
...   time.sleep(random.uniform(wait_low, wait_high))
...   print 'hello'+str(i)
... 
>>> def g(i):
...   time.sleep(random.uniform(wait_low, wait_high))
...   print 'hey'+str(i)
... 
>>> 

然后创建并启动地图

>>> p = Pool()
>>> r = p.map(f, range(10)); print "switch"; r = p.map(g, range(10)); print "I'm done"
hello6
hello2
hello1
hello0
hello5
hello8
hello3
hello4
hello7
hello9
switch
hey5
hey6
hey7
hey1
hey9
hey4
hey8
hey3
hey0
hey2
I'm done
>>> 

您可以在此处pathos获取https://github.com/uqfoundation