By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,825 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,825 IT Pros & Developers. It's quick & easy.

Context manager for files vs garbage collection

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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.

Jun 27 '08 #2

P: n/a
On Jun 16, 8:24 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>
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.
Jun 27 '08 #3

P: n/a
Floris Bruynooghe <fl***************@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)
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.