Re: Context manager for files vs garbage collection



Floris Bruynooghe <floris.bruynooghe@xxxxxxxxx>:

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)
.



Relevant Pages

  • Re: Computer totally freezing after a few min
    ... likely that they would be causing intermittent problems. ... You said you took out all but one of the memory sticks...but did you ... last resort especially if it sounds like the problem is the caps. ... "Freedom, true freedom, is nothing more than intellectual advantage over others." ...
    (alt.2600)
  • Re: differences between MADV_FREE and MADV_DONTNEED
    ... > relying on errors or no behaviour on file-backed memory? ... would break freebsd apps that might expect a noop instead. ... apparently posix too) app on a linux system that will corrupt memory ... utilization, a breakage not. ...
    (Linux-Kernel)
  • Re: BNs 2006 AGM
    ... People who were not at the AGM will just have to wait for the official ... It will be written by Michael Farrar, relying on memory and a few ... It appears that Paul was not ...
    (uk.rec.naturist)
  • Re: quarter square triangles
    ... AliceW - Royal Cybrarian ... Hug them while they're here! ... Just relying on memory, one of these days I would do a block using ...
    (rec.crafts.textiles.quilting)
  • Re: What file format for several hundred family photos?
    ... > Thanks for the detailed and helpful reply Clyde. ... > I have been relying on an external hard drive (and memory sticks) for all ... harddrives "go bad" after a while, lifespan of maybe 2-4 years at best ...
    (alt.graphics.photoshop)