A中的全局变量g_tmp更改。在线程B中输出g_tmp时,它保持不变。
from time import sleep, time
from multiprocessing import Process, current_process
g_tmp=[0,0]
def A():
while True:
sleep(20)
global g_tmp
print(g_tmp)
print(str(g_tmp[0]+g_tmp[1]))
def B():
global g_tmp
while True:
sleep(2)
g_tmp[0]+=1
print(g_tmp)
def main():
threadA = Process(target=A, name='A')
threadA.start()
threadB = Process(target=B, name='B')
threadB.start()
threadB.join()
if __name__ == "__main__":
main()
如何使对全局变量的更改在所有线程中可见?例如,在前20秒后,显示[10.0] 10?