I start a thread that runs exec and sets the global variable to True, meaning that the thread finished. The main thread that creates this new thread runs in a loop while the global var is False. The problem is that the exec job is also running in a loop and it's not known when will it finish and right now it seems that exec never finishes if i put a condition with an sys.exit somewhere. If I put the os._exit then everything finishes, the exec, the new thread and the main thread. But i need something that only stops exec and moves on (and the new thread finishes by itself). Is there any way to do this? I cannot just use return, because i need to exit deep inside some function call in exec.