在进程之间通过引用传递

时间:2016-02-29 22:14:55

标签: python

我有一个对象:

from multiprocessing import Pool
import time

class ASYNC(object):
    def __init__(self, THREADS=[]):
        print('do')
        pool = Pool(processes=len(THREADS))
        self.THREAD_POOL = {}
        thread_index = 0
        for thread_ in THREADS:
            self.THREAD_POOL[thread_index] = {
                'thread': thread_['thread'],
                'args': thread_['args'],
                'callback': thread_['callback']
            }
            self.THREAD_POOL[thread_index]['running'] = True
            pool.apply_async(self.run, [thread_index], callback=thread_['callback'])
            thread_index += 1
    def run(self, thread_index):
        print('enter')
        while(self.THREAD_POOL[thread_index]['running']):
            print("loop")
            self.THREAD_POOL[thread_index]['thread'](self.THREAD_POOL[thread_index])#HERE
            time.sleep(1)
        self.THREAD_POOL[thread_index]['running'] = False
    def wait_for_finish(self):
        for pool in self.THREAD_POOL:
            while(self.THREAD_POOL[pool]['running']):
                print("sleep" + str(self.THREAD_POOL[pool]['running']))
                time.sleep(1)
def x(pool):#HERE
    print(str(pool))
    if(pool['args'][0] >= 15):
        pool['running'] = False
    pool['args'][0] += 1


def y(str):
    print("done")

A = ASYNC([{'thread': x, 'args':[10], 'callback':y}])

print("start")
A.wait_for_finish()

我在将self.THREAD_POOL[thread_index]作为对def x(pool)

的引用时遇到问题

我需要x(pool)来更改对象中变量的值。

如果我检查wait_for_finish中的值,则对象不会更改。

通过引用传递对象:(已测试并正常工作)

x = {"1":"one", "2","two"}
def test(a):
     a["1"] = "ONE"
print(x["1"])#outputs ONE as expected

这意味着python中的字典是通过引用传递的;那么,为什么在我的代码中它是通过值传递的呢?

@DevShark

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print num.value
    print arr[:]

根据文档,除非绝对需要,否则不应该这样做。我决定不使用它。 https://docs.python.org/2/library/multiprocessing.html#multiprocessing.JoinableQueue

而不是我将做:

from multiprocessing import Pool
import time

class ASYNC(object):
    def __init__(self, THREADS=[]):
        print('do')
        pool = Pool(processes=len(THREADS))
        self.THREAD_POOL = {}
        thread_index = 0
        for thread_ in THREADS:
            self.THREAD_POOL[thread_index] = {
                'thread': thread_['thread'],
                'args': thread_['args'],
                'callback': thread_['callback']
            }
            self.THREAD_POOL[thread_index]['running'] = True
            pool.apply_async(self.run, [thread_index], callback=thread_['callback'])
            thread_index += 1
    def run(self, thread_index):
        print('enter')
        while(self.THREAD_POOL[thread_index]['running']):
            print("loop")
            self.THREAD_POOL[thread_index]['thread'](thread_index)
            time.sleep(1)
        self.THREAD_POOL[thread_index]['running'] = False
    def wait_for_finish(self):
        for pool in self.THREAD_POOL:
            while(self.THREAD_POOL[pool]['running']):
                print("sleep" + str(self.THREAD_POOL[pool]['running']))
                time.sleep(1)
def x(index):
    global A
    A.THREAD_POOL[index]
    print(str(pool))
    if(pool['args'][0] >= 15):
        pool['running'] = False
    pool['args'][0] += 1


def y(str):
    print("done")

A = ASYNC([{'thread': x, 'args':[10], 'callback':y}])

print("start")
A.wait_for_finish()

1 个答案:

答案 0 :(得分:1)

您正在以不同的流程运行您的功能。这就是多处理工作的方式。因此,无论您对该对象执行什么操作,都不会在其他进程中看到修改。

要在流程之间共享数据,请参阅评论中提及的the doc

  

可以使用Value或Array将数据存储在共享内存映射中。