当我运行下面的程序:用“与”关键字结合线程
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'
有什么办法与关键字的保证清理代码结合执行一个线程类?
你的意思是f.run()? – squiguy