首页 > 解决方案 > 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 = Truemain()

我是 Python 的新手,我知道线程是如何工作的,但也许这不是实现我要实现的目标的最佳方式。随意提出另一个解决方案,如果它更好的话。

提前致谢。

标签: pythonmultithreadingpython-multithreading

解决方案


推荐阅读