0

通过启动 main,我正在启动一个线程,该线程使与 opcua 服务器的连接保持活动状态(以及更多内容)。我现在想在这个线程中打开一个函数,但我不想再次导入所有内容(因为它需要很长时间)。

if __name__ == "__main__": 它正在工作,但是当我运行第二个脚本goIntoThread.py时,它不工作。显然是因为我没有导入模块......

我有哪些选项可以触发例如 thd.doSomethingInThread() 而无需再次导入所有内容?

非常感谢!

主文件

import time

def importOnlyMain():
    global KeepConnected
    from keepConnected import KeepConnected

if __name__ == "__main__":
    importOnlyMain()
    global thd
    thd = KeepConnected()
    thd.start()
    time.sleep(3)
    thd.doSomethingInThread()

def goIntoThread():
    print("Going to Thread")
    thd.doSomethingInThread()

goIntoThread.py

import main

main.goIntoThread()

复制评论:我收到以下错误:

thd.setBool() 
NameError: global name 'thd' is not defined 
4

0 回答 0