0

I currently have a main function being run in Python on the main thread. This main function creates an additional thread called ArtificialPlayerControllerSynthesis to run a background process. I would like to pause the main thread for a second or two to allow the background process to finish before continuing in the main thread. However, all solutions to this issue that I can find, such as from Pausing a thread using threading class, require passing an event object as an argument to the thread I want to pause. This is not possible in my case, or would at the very least require restructuring my code and moving my main function to a new thread. Is it possible to simply pause the main thread? I am new to Python and threading, thanks in advance for yall's help.

Evan157
  • 11
  • 1
  • 2
  • 1
    You can use `other_thread.join()` to block the main thread until `other_thread` completes https://docs.python.org/3/library/threading.html#threading.Thread.join – Iain Shelvington May 05 '20 at 00:36
  • If you want a guarantee that the background process has finished, then you _need_ to do some form of explicit synchronization. If you merely want to pause for 2 seconds, `time.sleep(2)` will do that. – Tim Peters May 05 '20 at 00:41

1 Answers1

1
thread = threading.Thread(target=some_func)
thread.start()
do_some_stuff_now()
thread.join() # blocks until thread finishes
do_some_stuff_later()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179