Context manager for files vs garbage collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Floris Bruynooghe

    Context manager for files vs garbage collection

    Hi

    I was wondering when it was worthwil to use context managers for
    file. Consider this example:

    def foo():
    t = False
    for line in file('/tmp/foo'):
    if line.startswith ('bar'):
    t = True
    break
    return t

    What would the benefit of using a context manager be here, if any?
    E.g.:

    def foo():
    t = False
    with file('/tmp/foo') as f:
    for line in f:
    if line.startswith ('bar'):
    t = True
    break
    return t

    Personally I can't really see why the second case would be much
    better, I've got used to just relying on the garbage collector... :-)
    But what is the use case of a file as a context manager then? Is it
    only useful if your file object is large and will stay in scope for a
    long time?

    Regards
    Floris
  • Bruno Desthuilliers

    #2
    Re: Context manager for files vs garbage collection

    Floris Bruynooghe a écrit :
    Hi
    >
    I was wondering when it was worthwil to use context managers for
    file. Consider this example:
    >
    def foo():
    t = False
    for line in file('/tmp/foo'):
    if line.startswith ('bar'):
    t = True
    break
    return t
    >
    What would the benefit of using a context manager be here, if any?
    E.g.:
    >
    def foo():
    t = False
    with file('/tmp/foo') as f:
    for line in f:
    if line.startswith ('bar'):
    t = True
    break
    return t
    >
    Personally I can't really see why the second case would be much
    better, I've got used to just relying on the garbage collector... :-)
    IIRC (please someone correct me if I'm wrong), proper release of file
    resources as soon as the file object gets out of scope is not garanteed
    in the language spec and is implementation dependant.

    Comment

    • Benjamin

      #3
      Re: Context manager for files vs garbage collection

      On Jun 16, 8:24 am, Bruno Desthuilliers <bruno.
      42.desthuilli.. .@websiteburo.i nvalidwrote:
      >
      IIRC (please someone correct me if I'm wrong), proper release of file
      resources as soon as the file object gets out of scope is not garanteed
      in the language spec and is implementation dependant.
      Right. Resources are freed in CPython right after they drop out of
      scope. This is not true in other implementations , so it's best to be
      explicit.

      Comment

      • Sebastian \lunar\ Wiesner

        #4
        Re: Context manager for files vs garbage collection

        Floris Bruynooghe <floris.bruynoo ghe@gmail.com>:
        I was wondering when it was worthwil to use context managers for
        file. Consider this example:
        >
        def foo():
        t = False
        for line in file('/tmp/foo'):
        if line.startswith ('bar'):
        t = True
        break
        return t
        Using this code inside a jthon web application might hit the resource limits
        of the underlying operating system.

        While CPython has a fairly deterministic garbage collector, the JVM gc is
        non-deterministic, especially inside the server vm. It keeps objects
        around for quite a long time and only frees them, if available memory runs
        low or the application is idle. Since file objects don't consume much
        memory, they might be hanging around for quite some time still claiming
        resources, which are a rare thing on many restricted server environments.

        Relying on garbage collection on Python means relying on a non-portable
        implementation detail.

        --
        Freedom is always the freedom of dissenters.
        (Rosa Luxemburg)

        Comment

        Working...