-1

is there a way to shift to main thread from a background thread, for example;

import threading

def func(txt):
    if txt == 'foo':
        ## must do the processing in the main thread
        pass

    else:
        print(txt)


thread = threading.Thread(target = func, args = ('hello',))
thread.start()

Thanks in advance :)

Arjun S
  • 40
  • 5

1 Answers1

0

You could have a variable in mainthread which will be changed in func. Then check in main the status of the variable.

If foo, then do something.

Gamsner
  • 117
  • 1
  • 9
  • Yeah, okay, imagine having an infinite loop in the Thread "thread" , and even if you call some other function inside the Thread "thread" it will execute in the Thread "thread" and not in "main_thread", I need a way to completely change to main thread. – Arjun S Aug 22 '20 at 12:01
  • Sorry misunderstood me. Set a variable in main thread which you change in thread. For this need probably global variablename – Gamsner Aug 22 '20 at 16:35