Python:线程中的运行函数不会修改current_thread()
问题描述:
我目前正在努力弄清楚线程如何在python中工作。Python:线程中的运行函数不会修改current_thread()
我有以下代码:
def func1(arg1, arg2):
print current_thread()
....
class class1:
def __init__():
....
def func_call():
print current_thread()
t1 = threading.Thread(func1(arg1, arg2))
t1.start()
t1.join()
我注意到的是,这两个打印输出相同的事情。为什么线程没有改变?
答
您正在执行的功能,而不是传递它。试试这个:
t1 = threading.Thread(target = func1, args = (arg1, arg2))
答
您呼叫的功能是提供给Thread
构造函数之前。 此外,您将它作为错误的参数(线程构造函数的第一个位置参数是group
)。假设func1
返回None
你正在做什么等于呼叫threading.Thread(None)
或threading.Thread()
。 这在threading docs中有更详细的解释。
为了让你的代码工作试试这个:
t1 = threading.Thread(target=func1, args=(arg1, arg2))
t1.start()
t1.join()
它的工作,非常感谢你! – 2013-03-17 12:37:54