【发布时间】:2012-02-11 19:56:29
【问题描述】:
我使用 Python 和线程已经有一段时间了,但我对回调仍有一些疑问。取以下代码:
import threading
def cb_func(data):
"""The callback function"""
print data
def th_func(callback):
"""The threaded function"""
# do some work here
callback('somedata')
thr = threading.Thread(target=th_func, args=(cb_func,)).start()
现在,根据这段代码,函数 cb_func 将在主线程中运行,还是在新创建的 (thr) 线程中运行?我之所以问,是因为我正在使用 GUI 工具包 (GTK),并且在以这种方式调用回调时偶尔会遇到 X 错误(和段错误)(是的,我知道 gobject.idle_add)。
提前感谢您,并对我的愚蠢问题感到抱歉。
【问题讨论】:
标签: python multithreading thread-safety