在下面的代码中,我期望输出结果是2,因为我在将函数分配给多进程池之前更改了config的值,但实际上我得到了5。我确定有一个很好的理由,但不确定如何解释。
from multiprocessing import Pool
config = 5
class Test:
def __init__(self):
print("This is init")
@classmethod
def testPrint(cls, data):
print(config)
print("This is testPrint")
return config
if __name__ == "__main__" :
pool = Pool()
config = 2
output = pool.map(Test.testPrint, range(10))
print(output)
输出
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
5
This is testPrint
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]