首页 > 解决方案 > 结合 .start() 的 Python3 线程不会创建连接属性

问题描述

这工作正常:

def myfunc():
    print('inside myfunc')

t = threading.Thread(target=myfunc)
t.start()
t.join()
print('done')

然而,虽然显然正确地创建和执行了线程:

def myfunc():
    print('inside myfunc')

t = threading.Thread(target=myfunc).start()
t.join()
print('done')

当它命中 join() 时生成以下致命错误:

AttributeError:“NoneType”对象没有“加入”属性

我会认为这些陈述是等价的。有什么不同吗?

标签: pythonpython-3.xpython-multithreading

解决方案


t = threading.Thread(target=myfunc).start()

threading.Thread(target=myfunc) 返回一个线程对象,但是 object.start() 返回 None。这就是为什么会有一个 AttributeError。


推荐阅读