python - 为什么全局变量在线程中有不同的值?
问题描述
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?
解决方案
推荐阅读
- android - Im having trouble populating ListTile in ListView.builder from database
- servicestack - AutoQuery/OrmLite incorrect total value when using joins
- java - 如何从 Json 字符串动态创建按钮
- c++ - Is there any way to access QByteArray member which is member of a union
- python-3.x - 从scrapy爬取更新sqlite DB中的数据的问题
- sitecore - 挂钩到项目选择操作
- hibernate - Flyway & MariaDB:SQLException 外键约束的格式不正确
- variables - 如何将命令输出保存到变量
- algorithm - 为什么此代码通过 Prim 的算法生成错误的最小生成树?
- python - 如何忽略某些方法而不是其他方法的 __repr__()