无法访问 python 中的全局变量
Can't access global variable in python
我在下面的代码中使用 python 中的多处理库:
from multiprocessing import Process
import os
from time import sleep as delay
test = "First"
def f():
global test
print('hello')
print("before: "+test)
test = "Second"
if __name__ == '__main__':
p = Process(target=f, args=())
p.start()
p.join()
delay(1)
print("after: "+test)
本应改变test
的值,所以最后test的值必须是Second
,但该值没有改变,仍然是First
。
这是输出:
hello
before: First
after: First
您看到的行为是因为 p 是新的 进程,而不是新的 线程。当您生成一个新进程时,它会完全复制您初始进程的状态,然后开始并行执行。当您生成一个线程时,它会与您的初始线程共享内存。
由于进程有内存隔离,它们不会产生由读取和写入共享内存引起的竞争条件错误。但是,要将数据从子进程返回到父进程,您需要使用某种形式的进程间通信(如管道),并且由于它们分叉内存,因此生成它们的成本更高。与计算机科学一样,您必须做出权衡。
有关详细信息,请参阅:
- https://en.wikipedia.org/wiki/Process_(computing)
- https://en.wikipedia.org/wiki/Thread_(computing)
- https://en.wikipedia.org/wiki/Inter-process_communication
根据您实际要完成的任务,考虑改用线程。
全局状态未共享,因此子进程所做的更改无效。
原因如下:
Actually it does change the global variable but only for the spawned
process. If you would access it within your process you can see it. As
its a process your global variable environment will be initialized but
the modification you make will be limited to the process itself and
not the whole.
试试这个它解释了发生了什么
from multiprocessing import Process
import os
from time import sleep as delay
test = "First"
def f2():
print ("f2:" + test)
def f():
global test
print ('hello')
print ("before: "+test)
test = "Second"
f2()
if __name__ == '__main__':
p = Process(target=f, args=())
p.start()
p.join()
delay(1)
print("after: "+test)
如果您真的需要使用他们的另一种方法从过程中进行修改,请阅读这篇文章doc or post它可能会对您有所帮助。
我在下面的代码中使用 python 中的多处理库:
from multiprocessing import Process
import os
from time import sleep as delay
test = "First"
def f():
global test
print('hello')
print("before: "+test)
test = "Second"
if __name__ == '__main__':
p = Process(target=f, args=())
p.start()
p.join()
delay(1)
print("after: "+test)
本应改变test
的值,所以最后test的值必须是Second
,但该值没有改变,仍然是First
。
这是输出:
hello
before: First
after: First
您看到的行为是因为 p 是新的 进程,而不是新的 线程。当您生成一个新进程时,它会完全复制您初始进程的状态,然后开始并行执行。当您生成一个线程时,它会与您的初始线程共享内存。
由于进程有内存隔离,它们不会产生由读取和写入共享内存引起的竞争条件错误。但是,要将数据从子进程返回到父进程,您需要使用某种形式的进程间通信(如管道),并且由于它们分叉内存,因此生成它们的成本更高。与计算机科学一样,您必须做出权衡。
有关详细信息,请参阅:
- https://en.wikipedia.org/wiki/Process_(computing)
- https://en.wikipedia.org/wiki/Thread_(computing)
- https://en.wikipedia.org/wiki/Inter-process_communication
根据您实际要完成的任务,考虑改用线程。
全局状态未共享,因此子进程所做的更改无效。
原因如下:
Actually it does change the global variable but only for the spawned process. If you would access it within your process you can see it. As its a process your global variable environment will be initialized but the modification you make will be limited to the process itself and not the whole.
试试这个它解释了发生了什么
from multiprocessing import Process
import os
from time import sleep as delay
test = "First"
def f2():
print ("f2:" + test)
def f():
global test
print ('hello')
print ("before: "+test)
test = "Second"
f2()
if __name__ == '__main__':
p = Process(target=f, args=())
p.start()
p.join()
delay(1)
print("after: "+test)
如果您真的需要使用他们的另一种方法从过程中进行修改,请阅读这篇文章doc or post它可能会对您有所帮助。