python2.7中的多处理模块导致某些问题 [英] Multiprocessing module in python2.7 causing some issue

查看:97
本文介绍了python2.7中的多处理模块导致某些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python2.7中使用了多处理模块,发现它存在一些问题,如解决该问题,这在我脑海中已荡然无存.

I am using multiprocessing module in python2.7 and found to have some issue with it, which is currently going out of my head, as in how to resolve it.

我正在使用的示例代码如下:-

The sample code that I am using is as follows:-

from multiprocessing import Pool

def func_eachcol(col_index):
    global list_of_lists

    for row in xrange(list_of_lists):
        list_of_lists[row][col_index] = 'Update Value'

class C1:
    def func_callmultiprocess(self):

        global list_of_lists
        col_len = len(cols)
        pool = Pool(col_len)
        for col in xrange(col_len):
            pool.apply_async(func_eachcol, args=(col))
        pool.close()
        pool.join()

        print list_of_lists

基本上,我正在做的是在每个列上打开一个新进程,并遍历list_of_lists的每一行,然后在该进程中对该行进行更新.

Basically, what i am doing, is on each col I am opening up a new process, and iterating through each row of list_of_lists, and then updating on that row,col in that very process.

我在这里看到的问题是,多处理结束后,我看不到更新的list_of_lists,实际上它是旧的list_of_lists.

The issue I am seeing here is that after multiprocessing is over, i cannot see the updated list_of_lists, infact it is the old list_of_lists.

但是当我看到在multiprocess_function(func_eachcol)内部时,然后我可以看到该索引(行,列)中的值正在更新.

but when i see, inside the multiprocess_function (func_eachcol), then I could see the values getting updated in that very index(row, col).

请对此问题提供一些补救措施.

Please provide some remedy to this problem.

添加了更多代码,以简化我想要实现的目标

Added one more code, to simple what I wanted to achieve

from multiprocessing import Pool

def func_eachcol(col_index):
    print "Hello",
    global list

    print list,
    list[col_index] = -list[col_index]
    print list

class C1:
    def func_callmultiprocess(self):

        global list 
        list = [1,2,3]

        col_len = len(list)

        pool = Pool(col_len)
        for col in xrange(col_len):
            pool.apply_async(func_eachcol, args=(col,))

        pool.close()
        pool.join()

        print list

c = C1()
c.func_callmultiprocess()


**Ouput**
Hello [1, 2, 3] [-1, 2, 3]
Hello [1, 2, 3] [1, -2, 3]
Hello [1, 2, 3] [1, 2, -3]
[1, 2, 3]

基本上我想要输出的最终是[-1,-2,-3].怎么可能??

Basically what i wanted the output in the end is [-1, -2, -3]. How is it possible.?

推荐答案

全局变量在多个进程之间不共享.您仅在子流程中操作全局列表,父流程将看不到更改.

Global variables are not shared between multiple processes; you are manipulating a global list in a sub-process only, the parent process won't see the changes.

查看有关共享状态的文档关于如何在流程之间传递信息.您可能希望Manager实例共享您的列表.

Review the documentation on sharing state for options on how to pass information between processes. You probably want a Manager instance to share your list.

这篇关于python2.7中的多处理模块导致某些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆