本文介绍了python:使用多处理时访问变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是新的多处理概念在python和我有问题访问变量时,我试图包括多处理在我的代码。对不起,如果Iam听起来天真,但我只是无法想出它。下面是我的场景的简单版本。 类数据: def __init __(self): self.data =data def datameth(self): print self.data print mainvar class First: def __init __ ): self.first =first def firstmeth(self):d = Data() d.datameth() print self.first def mymethod():f = First() f.firstmeth() 如果__name__ =='__main__': mainvar = mainvar mymethod() 当我运行这个时,输出: data mainvar first pre> 但是当我尝试运行 mymethod()作为进程时 从多进程导入进程类数据: def __init __(self): self.data =data自定义(self): print self.data print mainvar class First: def __init __(self): self.first = first def firstmeth(self):d = Data() #print mainvar d.datameth() print self.first def mymethod():f = First() f.firstmeth() 如果__name__ =='__main__': mainvar =mainvar #mymethod()p = Process(target = mymethod) p.start() 我得到这样的错误: NameError:global name'mainvar'is未定义 重点是,Iam无法访问 mainvar 从第一类或数据类。 我在这里缺少什么? 编辑:实际上,在我的实际情况下,它不只是声明mainvar,它是返回值 如果__name__ =='__main__': ***其他一些东西** * mainvar = ***某些方法的返回值** p = Process(target = mymethod) p.start() pre> 编辑2:由于@dciriello在注释中提到,它在Linux中正常工作,但不是在Windows:(解决方案这是Windows的限制,因为它不支持 fork 。在Linux中分叉,它获得父进程状态的写时复制副本,因此 mainvar 在中定义,如果__name__ ==__main__ :将在那里,但在Windows上,通过重新导入程序的 __ main __ 模块创建子进程的状态。意味着 mainvar 在子元素中不存在,因为它只在中创建,如果__name__ ==__main __守卫。所以,如果你需要在子进程中访问 mainvar ,你唯一的选择是显式地将它作为 mymethod 构造函数中的code> mainvar =无论p = Process(target = mymethod,args =(mainvar,)) 最佳做法在 多处理 docs :注意如果有。因为你的全局变量被声明在如果__name__ ==__main __: guard,它甚至不会显示在孩子中。 I am new to multiprocessing concepts in python and I have problem accessing variables when I try to include multiprocessing in my code. Sorry if Iam sounding naive, but I just cant figure it out. Below is a simple version of my scenario.class Data: def __init__(self): self.data = "data" def datameth(self): print self.data print mainvarclass First: def __init__(self): self.first = "first" def firstmeth(self): d = Data() d.datameth() print self.firstdef mymethod(): f = First() f.firstmeth()if __name__ == '__main__': mainvar = "mainvar" mymethod()When I run this, its running fine and gives the output: datamainvarfirstBut when I try to run mymethod()as a processfrom multiprocessing import Processclass Data: def __init__(self): self.data = "data" def datameth(self): print self.data print mainvarclass First: def __init__(self): self.first = "first" def firstmeth(self): d = Data() #print mainvar d.datameth() print self.firstdef mymethod(): f = First() f.firstmeth()if __name__ == '__main__': mainvar = "mainvar" #mymethod() p = Process(target = mymethod) p.start()I get an error like this: NameError: global name 'mainvar' is not definedThe point is, Iam not able to access mainvar from inside First class or Data class. What am I missing here?Edit:Actually in my real scenario, it is not just declaring mainvar, it is the return value of a method after some processing.if __name__ == '__main__': ***some other stuff*** mainvar = ***return value of some method** p = Process(target = mymethod) p.start()Edit 2:As @dciriello mentioned in comments, It is working fine in Linux but not in Windows :( 解决方案 This is a limitation of Windows, because it doesn't support fork. When a child process is forked in Linux, it gets a copy-on-write replica of the parent's processes state, so the mainvar you defined inside if __name__ == "__main__": will be there. However, on Windows, the child process' state is created by re-importing the __main__ module of the program. This means that mainvar doesn't exist in the children, because it's only created inside the if __name__ == "__main__" guard. So, if you need to access mainvar inside a child process, your only option is to explicitly pass it to the child as an argument to mymethod in the Process constructor:mainvar = "whatever"p = Process(target=mymethod, args=(mainvar,))This best-practice is mentioned in the multiprocessing docs:Notice the bold part - though it's not quite spelled out, the reason it helps with Windows compatibility is because it helps avoid the exact issue you're seeing.This is also covered in the section of the docs that talks specifically about Windows limitations caused by the lack of fork:Note the "if any". Because your global variable is declared inside the if __name__ == "__main__": guard, it doesn't even show up in the child. 这篇关于python:使用多处理时访问变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 13:31