def foo(var, new_value):
var = new_value
x = 1
multiprocessing.Process(target=foo, args=(x,2))
p.start()
p.join()
print(x)
我希望print(x)
的输出是2,但实际上我得到了1。如何为现有变量分配新值?
您需要使用多处理共享值;
import multiprocessing
import ctypes
def foo(var, new_value):
var.value = new_value
x = multiprocessing.Value(ctypes.c_int, 1)
print(x.value)
p = multiprocessing.Process(target=foo, args=(x,2))
p.start()
p.join()
print(x.value)