Context manager for files vs garbage collection

F

Floris Bruynooghe

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
 
B

Bruno Desthuilliers

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

Benjamin

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

Sebastian \lunar\ Wiesner

Floris Bruynooghe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top