473,385 Members | 1,707 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,385 software developers and data experts.

Resource cleanup

I'm thinking of using Tim Peters' excellent approach to resource clean
up see

http://mail.python.org/pipermail/pyt...il/063425.html
class _RealTypeResourceCleaner:
def __init__(self, *resources):
self.resources = resources

def __del__(self):
if self.resources is not None:
for r in self.resources:
r.close()
self.resources = None

# and typically no other methods are needed, or desirable, in
# this helper class

class RealType:
def __init__(*args):
...
# and then, e.g.,
self.cleaner = _ResourceCleaner(resource1, resource2)
but am wondering exactly what 'resources' are left available when the
r.close method is called in the __del__ method of RealTypeResourceCleaner.

In particular, can I rely on the module globals of r still being present
if the RealType instance is going away because the main script has
terminated, ie if the r.close method refers to a global function is it
guaranteed to be available when the close is called?

I guess I must be asking if referring to a global in a method is
actually a reference to that global or does the reference only occur
when the code is executed?

I have a vague feeling that I came across problems in the past about the
order in which modules were finalized.
--
Robin Becker
Dec 3 '06 #1
2 1241
but am wondering exactly what 'resources' are left available when the
r.close method is called in the __del__ method of RealTypeResourceCleaner.

In particular, can I rely on the module globals of r still being present
if the RealType instance is going away because the main script has
terminated, ie if the r.close method refers to a global function is it
guaranteed to be available when the close is called?

I guess I must be asking if referring to a global in a method is
actually a reference to that global or does the reference only occur
when the code is executed?

I have a vague feeling that I came across problems in the past about the
order in which modules were finalized.
I'm a bit on unsure ground here - so take it with a grain of salt.

It is for sure that only executing code will refer to a global - the
mere mention of anything can't possibly create a reference (in python at
least) - consider this simple example:

import random

def foo():
print schroedingers_cat

if random.random() .5:
schroedingers_cat = "I'm alive!"

foo()
So I presume it can very well happen that you will lose a module when
trying to finalize.

So most probably it is the cleverest solution to make the cleaner as
self-contained as possible, by storing explicit references to things you
might need in the instance itself. But I'm not sure if the transitivity
of dependencies might not kick your ass somewhere anyhow.

All in all an interesting topic - I'd be looking forward to more
insights, and very much liked the link you gave us.
Diez
Dec 3 '06 #2
Diez B. Roggisch wrote:
........
>>
I have a vague feeling that I came across problems in the past about
the order in which modules were finalized.

I'm a bit on unsure ground here - so take it with a grain of salt.

It is for sure that only executing code will refer to a global - the
mere mention of anything can't possibly create a reference (in python at
least) - consider this simple example:

import random

def foo():
print schroedingers_cat

if random.random() .5:
schroedingers_cat = "I'm alive!"

foo()
yes I guess although foo must know that schroedingers_cat is global it
doesn't need to bring it into existence so would fail 50% of the time :)
>
So I presume it can very well happen that you will lose a module when
trying to finalize.

So most probably it is the cleverest solution to make the cleaner as
self-contained as possible, by storing explicit references to things you
might need in the instance itself. But I'm not sure if the transitivity
of dependencies might not kick your ass somewhere anyhow.
I think this must be the right approach. Either the resources or the
cleaner should hold everything required for cleanup. Probably the
resource is the best place.

--
Robin Becker
Dec 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Julie | last post by:
(I searched the FAQ, but didn't find anything relevant -- if there is, please post a link.) Is there a design paradigm that indicates that a class that manages an external resource should...
5
by: John Perks and Sarah Mount | last post by:
When handling resources in Python, where the scope of the resource is known, there seem to be two schools of thought: (1) Explicit: f = open(fname) try: # ... finally: f.close()
5
by: Dennis | last post by:
When I instantiate a Brush resource such as a solidbrush, do I need to dispose the resource when I'm finished or will it automatically be disposed when the function/sub it's used in ends? Thank...
0
by: Mythran | last post by:
I wrote some code that is supposed to enumerate through the specified file's win32 resources and return a string-array of all icon names. When it runs, it returns a string-array with a bunch of...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.