当我运行以下程序时:
import threading
class foo(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def __enter__(self):
print "Enter"
def __exit__(self, type, value, traceback):
print "Exit"
def run():
print "run"
if __name__ == "__main__":
with foo() as f:
f.start()
我得到这个作为输出
C:\>python test2.py
Enter
Exit
Traceback (most recent call last):
File "test2.py", line 17, in <module>
f.start()
AttributeError: 'NoneType' object has no attribute 'start'
有没有办法将 with 关键字的保证清理代码执行与线程类结合起来?