我使用的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
值,以便在最后一次测试的值必须是Second
,但价值不会改变,并保持First
。 这里是输出:
hello
before: First
after: First