V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

Python 文件对象忘了 f.close() 会出什么问题? f = Image.open() 不用 close 方法吗?

  •  
  •   miniyao · Jan 5, 2018 · 9566 views
    This topic created in 3044 days ago, the information mentioned may be changed or developed.

    要是忘用 f.close() 或 with open() as f 关闭对象,会出什么问题吗?

    发现 PIL 的 Image 不用关闭方法吗? f = Image.open(img_file) 就没找到 f.close() 的关闭方法,不知道是为什么,求指点

    15 replies    2018-01-06 16:05:57 +08:00
    TimePPT
        1
    TimePPT  
    PRO
       Jan 5, 2018 via iPhone
    问之前建议去查文档
    ech0x
        2
    ech0x  
       Jan 5, 2018   ❤️ 1
    好像是因为没有关闭文件的话,系统会先缓存起来,然后慢慢写入文件,这样的话如果在写入途中断电会造成写入文件的不完整。
    billlee
        3
    billlee  
       Jan 5, 2018   ❤️ 2
    不关闭文件就一直打开在那里了,相关数据结构会占用内存,在进程退出之前会造成文件删不掉,所在文件系统无法 unmount。同时一个进程能打开的文件描述符有数量上限,打开大量文件后就会打不开新文件。
    PIL 的 Image class 明明有 close() 方法啊
    https://pillow.readthedocs.io/en/5.0.0/reference/Image.html#PIL.Image.Image.close
    shihira
        4
    shihira  
       Jan 5, 2018   ❤️ 1
    在 python 里不 close 跟在 C 里不 close 是一样,可能导致的问题包括不限于:
    Windows 好像是自动加锁的,所以不能重复 open 咯
    文件内容不能即时 flush 到磁盘直到进程结束

    所以如果你线程比较多或者有可能变僵尸的话会造成一些很莫名其妙的错误。如果文件内容不大,能及时读到内存,读完就可以关了。Image 应该只管读,读完你还得手动关文件(关文件不是关图像)。
    ysc3839
        5
    ysc3839  
       Jan 6, 2018
    @shihira *nix 才是加锁的吧? Windows 的 CreateFile 可以指定 share mode。
    Arnie97
        6
    Arnie97  
       Jan 6, 2018 via Android
    crab
        7
    crab  
       Jan 6, 2018
    @shihira 不是句柄泄露或者文件占用吗
    shihira
        8
    shihira  
       Jan 6, 2018   ❤️ 1
    @ysc3839 *nix 的特色之一不就是不主动加锁,要手动 fcntl/flock 吗。win 我不太了解,但是要是搬出 CreateFile 这种高大全 API 也就谈不上什么自动不自动了。刚刚求证了一下 MSDN,有讲到 Files opened by fopen_s and _wfopen_s are not sharable,我指的所谓自动是指调用一些比较通用、标准的函数时的行为。
    binux
        9
    binux  
       Jan 6, 2018 via Android
    我怎么记得文件变量回收后会自动关闭
    jimzhong
        10
    jimzhong  
       Jan 6, 2018
    @ysc3839 Linux 默认是 shared mode.
    doubleflower
        11
    doubleflower  
       Jan 6, 2018 via Android
    以前国外论坛看过一个解释,如果是原版的 py 引用计数垃圾回收,变量用完后就回收,文件自然也就关闭了,所以不 close 也没什么大问题。如果是别的版本的 py 垃圾回收话关闭时间会延长,就会有问题。所以建议还是加上
    mimzy
        12
    mimzy  
       Jan 6, 2018 via Android
    If you don ’ t use “ with ”, when does Python close files? The answer is: It depends.

    http://blog.lerner.co.il/dont-use-python-close-files-answer-depends/
    guyskk0x0
        13
    guyskk0x0  
       Jan 6, 2018 via Android
    @binux GC 执行时间是不确定的,可能长时间不回收
    rffan
        14
    rffan  
       Jan 6, 2018
    找不到方法的时候就用 with 啊 这种类型自动 close。。。
    ml071987
        15
    ml071987  
       Jan 6, 2018
    Image 里面有__exit__,退出时调用了 close
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5829 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 97ms · UTC 02:42 · PVG 10:42 · LAX 19:42 · JFK 22:42
    ♥ Do have faith in what you're doing.