使用python执行paralel进程时返回值

时间:2016-01-11 15:00:07

标签: python queue global-variables python-multiprocessing

我一直在使用python进行多处理,并且我已经成功使用了队列,但是当进程仍在执行时,我需要监视一些变量(来自main)。

我知道使用全局变量不是一个好习惯,但即使这种方法也没有用。

任何人都可以指出我正确的方向吗?

提前致谢,

GCCruz

附录: 我发布了一个我想做的简单例子:

import multiprocessing
import time

def sampleprocess(array, count):
    '''process with heavy image processing in a loop'''
    for i in range(count)
        # Do processing on this array that outputs a given variable
        sample_variable= count*10 # I would like to monitor this variable  

if __name__ == '__main__':

    p = multiprocessing.Process(target=sampleprocess, args=(array,1000,))
    p.start()

    # continuously monitor the dummy variable that is being computed on the process
    while sample_variable < 1000
        time.sleep(0.1)
        print ' Still less than 1000'

2 个答案:

答案 0 :(得分:0)

一种选择是对共享数据对象使用多处理值和数组。 https://docs.python.org/2/library/multiprocessing.html#multiprocessing-managers

示例代码中的一个工作示例。如果你有一个以上 处理锁定将需要同步写入。

来自多处理导入Process,Value,Array,Lock 进口时间

def sampleprocess(s,count,lock):     '''在循环中进行大量图像处理的过程'''     对于范围内的i(count.value):         #在此阵列上处理输出给定变量

sample_variable = count * 10#我想监视这个变量

    with lock:
        s.value= i*10

如果名称 =='主要':

val = Value('i', 1000)
sample_variable = Value('i', 0)
lock = Lock()

p = Process(target=sampleprocess, args=(sample_variable, val, lock))
p.start()

# continuously monitor the dummy variable that is being computed on the process
while sample_variable.value < 1000:
    time.sleep(0.1)
    print ' Still less than 1000'

答案 1 :(得分:0)

以下是一些可能演示如何实施解决方案的程序:

示例1

from multiprocessing import *
import time


def main():
    array, loops = list(range(1000)), 1000
    variable = Value('I')
    p = Process(target=sample, args=(array, loops, variable))
    p.start()
    while variable.value < 1000:
        print('Still less than 1000')
        time.sleep(0.005)
    print('Must be at least 1000')
    p.join()
    print('Value is', variable.value)


def sample(array, loops, variable):
    for number in range(loops):
        variable.value = number * 10
    print('Sample is done')

if __name__ == '__main__':
    main()

示例2

from multiprocessing import *
import time


def main():
    processes = 10
    array, loops = list(range(1000)), 1000
    shared = Array('I', processes)
    p_array = []
    for index in range(processes):
        p = Process(target=sample, args=(array, loops, shared, index))
        p.start()
        p_array.append(p)
    while True:
        less_than_1000 = [p for p in enumerate(shared[:]) if p[1] < 1000]
        if less_than_1000:
            print(less_than_1000)
            time.sleep(0.001)
        else:
            break
    print('No process in less than 1000')
    for p in p_array:
        p.join()
    print(shared[:])


def sample(array, loops, p_array, index):
    time.sleep(1)
    for number in range(loops):
        time.sleep(0.001)
        p_array[index] = number * 10
    print('Sample is done')

if __name__ == '__main__':
    main()

示例3

from multiprocessing import *
import time


def main():
    array, loops = list(range(1000)), 1000
    with Manager() as manager:
        variable = manager.Value('I', 0)
        p = Process(target=sample, args=(array, loops, variable))
        p.start()
        while variable.value < 1000:
            print('Still less than 1000')
            time.sleep(0.005)
        print('Must be at least 1000')
        p.join()
        print('Value is', variable.value)


def sample(array, loops, variable):
    for number in range(loops):
        variable.value = number * 10
    print('Sample is done')

if __name__ == '__main__':
    main()

示例4

from multiprocessing import *
import time


def main():
    array, loops = list(range(1000)), 1000
    event = Event()
    p = Process(target=sample, args=(array, loops, event))
    p.start()
    event.wait()
    print('Must be at least 1000')
    p.join()


def sample(array, loops, event):
    for number in range(loops):
        if number >= 100 and not event.is_set():
            event.set()
            time.sleep(0.001)
    print('Sample is done')

if __name__ == '__main__':
    main()

正如您所看到的,有多种方法可以完成您要完成的任务。