在Python中,如何使用多处理从特定进程获取数据?

时间:2012-06-26 22:14:33

标签: python multiprocessing

import multiprocessing as mp
import time

def build(q):
    print 'I build things'
    time.sleep(10)
    #return 42
    q.put(42)

def run(q):
    num = q.get()
    print num

    if num == 42:
        print 'I run after build is done'
        return
    else:
        raise Exception("I don't know build..I guess")

def get_number(q):
    q.put(3)

if __name__ == '__main__':
    queue = mp.Queue()

    run_p = mp.Process(name='run process', target=run, args=(queue,))
    build_p = mp.Process(name='build process', target=build, args=(queue,))
    s3 = mp.Process(name='s3', target=get_number, args=(queue,))

    build_p.start()
    run_p.start()
    s3.start()

    print 'waiting on build'
    build_p.join(1) # timeout set to 1 second
    s3.join()

    print 'waiting on run'
    run_p.join()

    queue.close()
    print 'waiting on queue'
    queue.join_thread()
    print 'done'

我的目标是将buildrun发送给不同的工作人员,run必须从build获取结果才能继续。

基于您的帮助的上述修订代码实际上会返回异常,因为在s3有机会之前会返回build

队列前面的值现在是3.我们怎样才能确保从build进程得到答案?

感谢。

1 个答案:

答案 0 :(得分:2)

你的问题有点模糊......你所描述的问题听起来是同步的,所以3个过程有点矫枉过正。

假设您只是尝试传递要运行的值,您可以使用队列对象。

import multiprocessing as mp
import time

def build(q):
    print 'I build things'
    time.sleep(5)
    q.put(42)
    return 

def run(q):
    while True:
        num = q.get()
        if num == 42:
            print 'I run after build is done'
            return
        else:
            print 'not the right number...'

    def get_number():
        return 41

if __name__ == '__main__':
    queue = mp.Queue()

    run_p = mp.Process(name='run process', target=run, args=(queue,))
    build_p = mp.Process(name='build process', target=build, args=(queue,))

    run_p.start()
    build_p.start()

    print 'waiting on build'
    build_p.join()
    print 'waiting on run'
    run_p.join()
    queue.close()
    print 'waiting on queue'
    queue.join_thread()
    print 'done'