473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jun 27 '08 #1
3 1202
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
3
by: Jerry Sievers | last post by:
Hello. I have session files stored in a special tmp directory specific to a virtual host instance. The Max_lifetime is long 86400 (1 day) which I don't suppose is the problem. Anyway the...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
12
by: Marcelo Fernandez | last post by:
Hi ! I was wondering if there is tool that could tell me if some of my pointers are still alive when they are suppose to be dead (in case I forget to kill them...). As you have noticed, I am...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.