【问题标题】:Pausing the Main Thread in Python在 Python 中暂停主线程
【发布时间】:2020-08-19 13:38:23
【问题描述】:

我目前在 Python 的主线程上运行了一个 main 函数。这个主函数创建了一个名为ArtificialPlayerControllerSynthesis 的附加线程来运行后台进程。我想暂停主线程一两秒钟,以允许后台进程完成,然后再继续主线程。但是,我能找到的所有解决此问题的方法(例如来自Pausing a thread using threading class)都需要将事件对象作为参数传递给我要暂停的线程。在我的情况下这是不可能的,或者至少需要重组我的代码并将我的主函数移动到一个新线程。是否可以简单地暂停主线程?我是 Python 和线程的新手,在此先感谢您的帮助。

【问题讨论】:

标签: python multithreading python-multithreading


【解决方案1】:
thread = threading.Thread(target=some_func)
thread.start()
do_some_stuff_now()
thread.join() # blocks until thread finishes
do_some_stuff_later()

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多