5
我對此上測試下面的代碼的一個問題:何時在python中釋放函數堆棧數據?
1,
def file_close_test():
f = open('/tmp/test', 'w+')
if __name__ == '__main__':
file_close_test()
# wait to see whether file closed.
import time
time.sleep(30)
2,
def file_close_on_exc_test():
f = open('/tmp/test', 'w+')
raise Exception()
def exception_wrapper():
try:
file_close_on_exc_test()
except:
pass
# wait to see whether file closed.
import time
time.sleep(10)
if __name__ == '__main__':
exception_wrapper()
import time
time.sleep(30)
- 文件對象時關閉file_close_test退出,因爲沒有參考它。
- 發生異常後,文件對象沒有關閉,所以我認爲相關的堆棧數據沒有發佈。
- 後exception_wrapper退出,文件自動關閉。
你能解釋一下這個給我嗎?謝謝。
這也不難:'開放('/ tmp/test','w +')爲f:' –
它幫助我很多,謝謝:) – yancl