E.B*_*ian 0 python multithreading thread-safety threadpool
我是 Python 新手。我正在尝试做某事,但不确定是否可能。我想创建一个运行 1 个函数的线程,并在完成后运行另一个函数。
例如
thread.start_new_thread( func1 )
//Run this thread only after the first one was finished
thread.start_new_thread( func2 )
Run Code Online (Sandbox Code Playgroud)
是否可以用 1 个线程来完成?或者我需要创建 2 个线程?我应该怎么办?
如果您希望同一个线程运行这两个函数,您可以使用 func3 启动线程,该线程调用 func1,然后调用 func2。
def func3:
func1()
func2()
thread.start_new_thread(func3, ())
Run Code Online (Sandbox Code Playgroud)
另一方面,您可以使用“threading”库并启动一个运行 func1 的线程,等待它完成,然后启动一个运行 func2 的线程:
import threading
t = threading.Thread(target = func1)
t.start()
t.join() # Waits until it is finished
t = threading.Thread(target = func2)
t.start()
Run Code Online (Sandbox Code Playgroud)