How can I access a running thread

66 Views Asked by At

By starting the main, I´m starting a thread that keeps a connection to a opcua server alive (and a few things more). I now want to open a function inside this thread but I don´t want to import everything again (because it takes to long).

In if __name__ == "__main__": it is working, but when I run a second script goIntoThread.py, it is not working. Obviously because I didn´t import the modules...

What are my options to trigger e.g. thd.doSomethingInThread() without importing everything again?

Thnaks alot!

main.py

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()

Copy Comment: I get the following error:

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

There are 0 best solutions below