简体   繁体   English

在 Python 暂停主线程

[英]Pausing the Main Thread in Python

I currently have a main function being run in Python on the main thread.我目前有一个主要的 function 在主线程上运行在 Python 中。 This main function creates an additional thread called ArtificialPlayerControllerSynthesis to run a background process.这个主要的 function 创建了一个名为 ArtificialPlayerControllerSynthesis 的附加线程来运行后台进程。 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.但是,我能找到的所有解决此问题的方法,例如使用线程暂停线程 class ,都需要将事件 object 作为参数传递给我要暂停的线程。 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.这在我的情况下是不可能的,或者至少需要重构我的代码并将我的主 function 移动到一个新线程。 Is it possible to simply pause the main thread?是否可以简单地暂停主线程? I am new to Python and threading, thanks in advance for yall's help.我是 Python 和线程的新手,在此先感谢大家的帮助。

thread = threading.Thread(target=some_func)
thread.start()
do_some_stuff_now()
thread.join() # blocks until thread finishes
do_some_stuff_later()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM