[英]Python3 threading combining .start() doesn't create the join attribute
这很好用:
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' object 没有属性 'join'
我本以为这些陈述是等价的。 有什么不同吗?
t = threading.Thread(target=myfunc).start()
threading.Thread(target = myfunc)返回一个线程对象,但object.start()返回None。 这就是存在AttributeError的原因。
自 Python 3.8 和海象运算符的引入:=
你可以使用;
(t := threading.Thread(target=myfunc)).start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.