我喜欢将函数转换为线程而无需定义类的不必要行的能力。我知道_thread,但看来您不应该使用_thread。python 3是否有等效于thread.start_new_thread的良好实践?
问问题
35693 次
2 回答
43
threading.Thread(target=some_callable_function).start()
或者如果你想传递参数,
threading.Thread(target=some_callable_function,
args=(tuple, of, args),
kwargs={'dict': 'of', 'keyword': 'args'},
).start()
于 2011-06-12T00:03:23.540 回答
5
不幸的是,没有直接的等价物,因为 Python 3 旨在比 Python 2 更便携,并且_thread
接口被视为太低级,无法实现此目的。
在 Python 3 中,最佳实践通常是使用threading.Thread(target=f...)
. 这使用不同的语义,但由于该接口更容易移植到其他 Python 实现,因此是首选。
于 2011-06-12T00:03:50.240 回答