gpt4 book ai didi

python - 有没有办法在Python中跨线程添加变量

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:49 26 4
gpt4 key购买 nike

有没有一种方法可以让我可以在事件线程中使用单个变量,如下所示

count = 0
threadA(count)
threadB(count)

threadA(count):
#do stuff
count += 1
threadB(count):
#do stuff
print count

这样 count 就会打印出 1?我更改了线程 A 中的变量,它反射(reflect)到了另一个线程?

最佳答案

您的变量 count 已可供所有线程使用。但您需要同步对其的访问,否则您将丢失更新。考虑使用锁来保护对计数的访问。

关于python - 有没有办法在Python中跨线程添加变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com