Python - 运行 多函数 1 线程

Python - Run multi functions with 1 thread

我是 Python 的新手。我正在尝试做某事,但不确定是否可行。 我想创建一个 运行 有 1 个函数的线程,然后在它完成后 运行 另一个函数。

例如

thread.start_new_thread( func1 )
//Run this thread only after the first one was finished
thread.start_new_thread( func2 )

是否可以用 1 个线程来完成?或者我需要创建 2 个线程? 我该怎么办?

如果您想要使用完全相同的线程 运行 这两个函数,您可以只使用调用 func1 然后调用 func2 的 func3 启动线程。

def func3:
    func1()
    func2()

thread.start_new_thread(func3, ())

另一方面,您可以使用 "threading" 库并启动一个 运行s func1 的线程,等到它完成后再启动一个 运行s 的线程func2:

import threading
t = threading.Thread(target = func1)
t.start()
t.join() # Waits until it is finished
t = threading.Thread(target = func2)
t.start()