python - Python将变量传递给从另一个文件(脚本)创建的线程
问题描述
我有两个文件:
线程模块.py
def main():
print("This is the thread!")
while 1:
*do something to set statistic_flag = True*
if __name__ == '__main__':
main()
主代码.py
import threadmodule
statistic_flag = True
t = threading.Thread(target=tbot.main)
t.daemon = True
t.start()
while 1:
if(statistic_flag == True):
print "flag was True"
statistic_flag = False
我想在threadmodule.py中做一些类似的事情,这样就可以打印主代码了statistic_flag = True
。main()
我是 Python 的新手,我知道线程是如何工作的,但也许这不是实现我要实现的目标的最佳方式。随意提出另一个解决方案,如果它更好的话。
提前致谢。
解决方案
推荐阅读
- vue-cli-3 - 使用 vue-cli-service build 时是否可以获得嵌套的 .vue 文件?
- postgresql - 有没有办法自动监控和终止正在静默进行的 AWS ECS 任务?
- javascript - 如何在 Vue 中更改道具时调用 Axios?
- javascript - 未捕获的 ReferenceError:未定义 selectAll
- django - django.urls.exceptions.NoReverseMatch:找不到“account_inactive”的反向。“account_inactive”不是有效的视图函数或模式名称
- java - 我的代码有什么问题?我无法打印条形码或二维码
- google-api - 是否有“人们也问”的谷歌 API?
- r - 按组删除低于特定行号/条件的行
- php - 这是在 laravel 中构建模板/布局系统的好方法吗?将数据保存到数据库会更好吗?
- python - Python中的函数组合运算符