小编yan*_*ncl的帖子

什么时候在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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)
  1. file_close_test退出时文件对象关闭,因为没有引用它.
  2. 引发异常后,文件对象没有关闭.所以我认为相关的堆栈数据没有被释放.
  3. exception_wrapper退出后,文件自动关闭.

你能帮我解释一下吗?谢谢.

python stack-frame

5
推荐指数
1
解决办法
167
查看次数

标签 统计

python ×1

stack-frame ×1