python - 结合 .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”对象没有“加入”属性
我会认为这些陈述是等价的。有什么不同吗?
解决方案
t = threading.Thread(target=myfunc).start()
threading.Thread(target=myfunc) 返回一个线程对象,但是 object.start() 返回 None。这就是为什么会有一个 AttributeError。
推荐阅读
- c# - System.Xml.XmlException:不能对具有子元素的元素调用 ReadElementContentAs() 方法
- python - 如何将焦点更改为pygame窗口?
- makefile - Gnu make:运行shell命令并使用退出代码设置全局变量
- javascript - RxJS / Angular:在订阅之前捕获 FormControl 的 valueChanges
- flutter - 当 showBottomSheet 包含可滚动小部件时,如何使 showBottomSheet 继续滑动?
- typo3 - powermail:没有强制选项的自定义验证器
- javascript - 如何使用 Jquery 模板加载多个模板?
- node.js - 如何在不同的 cwd 中运行 nodemon?
- ios - 是否可以在可本地化的字符串中自定义某些单词?
- java - Spring boot,Caffeine 缓存作为令牌存储,刷新和驱逐