Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Logging in __del__()

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


robert.rawlins at thinkbluemedia

Jul 15, 2008, 4:32 AM

Post #1 of 6 (502 views)
Permalink
Logging in __del__()

Guys,



I'm trying to help trace when instances of particular classes are being
destroyed by the garbage collector and thought the cleanest way would be to
implement a logging call in __del__() on the class. However, I'm having an
issue.



I inject a logger instance into my class upon construction and set it to
self.logger, I then use this within the class to log actions going on
within, works fine.



Now, when I make the call in the __del__() method like so:



def __del__(self):

# Log the classes destruction.

self.logger.debug("Class Instance Destroyed")



I then get the following exception thrown when running my code:



Traceback (most recent call last):

File "/usr/lib/python2.5/logging/handlers.py", line 73, in emit

if self.shouldRollover(record):

File "/usr/lib/python2.5/logging/handlers.py", line 147, in shouldRollover

self.stream.seek(0, 2) #due to non-posix-compliant Windows feature

ValueError: I/O operation on closed file



Does anyone have any ideas as to what I'm doing wrong here? Is this a known
issue which has a neat little work around?



Cheers,



Robert


fredrik at pythonware

Jul 15, 2008, 4:42 AM

Post #2 of 6 (472 views)
Permalink
Re: Logging in __del__() [In reply to]

Robert Rawlins wrote:

> I then get the following exception thrown when running my code:

When the application is running, or when it is shutting down?

> Traceback (most recent call last):
> File "/usr/lib/python2.5/logging/handlers.py", line 73, in emit
> if self.shouldRollover(record):
> File "/usr/lib/python2.5/logging/handlers.py", line 147, in shouldRollover
> self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
> ValueError: I/O operation on closed file
>
> Does anyone have any ideas as to what I’m doing wrong here? Is this a
> known issue which has a neat little work around?

Python makes no guarantees that it will tear down your objects before it
tears down the library's objects (or the library itself). See e.g.

http://www.python.org/doc/essays/cleanup/

(old and probably somewhat outdated, but you get the idea)

I'd just put a try/except around it, and ignore any exceptions that may
occur.

</F>

--
http://mail.python.org/mailman/listinfo/python-list


robert.rawlins at thinkbluemedia

Jul 15, 2008, 5:51 AM

Post #3 of 6 (481 views)
Permalink
RE: Logging in __del__() [In reply to]

Hi Fredrik,

> When the application is running, or when it is shutting down?

This is interesting, I did a test where I explicitly destroyed the instance
using 'del my_instance' while the application was running and no error was
thrown.

It would see you are right, when the application ends it kills the logging
module before my classes. I think you're on the right approach just
try/except it and leave it be.

Am I right in thinking that Python destroys instances of classes when it
deems they are no longer needed? I shouldn't have to explicitly delete the
classes, right?

Thanks Fredrik,

Robert

--
http://mail.python.org/mailman/listinfo/python-list


vinay_sajip at yahoo

Jul 15, 2008, 6:56 AM

Post #4 of 6 (481 views)
Permalink
Re: Logging in __del__() [In reply to]

On Jul 15, 1:51 pm, "Robert Rawlins"
<robert.rawl...@thinkbluemedia.co.uk> wrote:
>
> Am I right in thinking that Python destroys instances of classes when it
> deems they are no longer needed? I shouldn't have to explicitly delete the
> classes, right?

Python uses reference counting with a cycle detector, but the
detector's behaviour is different if there are finalizers (__del__) -
see

http://www.python.org/doc/ext/refcounts.html

Regards,

Vinay Sajip
--
http://mail.python.org/mailman/listinfo/python-list


robert.rawlins at thinkbluemedia

Jul 16, 2008, 4:38 AM

Post #5 of 6 (461 views)
Permalink
RE: Logging in __del__() [In reply to]

Hi Vinay,

> Python uses reference counting with a cycle detector, but the
> detector's behaviour is different if there are finalizers (__del__) -
> see
>
> http://www.python.org/doc/ext/refcounts.html
>

Thank you for the link, that certainly explains a great deal.

So, am I right to assume that python will still handle its garbage disposal
if I implement __del__(), it just handles circular references in a slightly
different way, but to the same effect. Right?

Cheers,

Robert

--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

Jul 16, 2008, 7:35 AM

Post #6 of 6 (463 views)
Permalink
Re: Logging in __del__() [In reply to]

On Wed, 16 Jul 2008 12:38:50 +0100, Robert Rawlins wrote:

> So, am I right to assume that python will still handle its garbage disposal
> if I implement __del__(), it just handles circular references in a slightly
> different way, but to the same effect. Right?

No. Circular references in objects with a `__del__()` implementation are
not collected! Why are you using `__del__()` anyway? Are you aware of
the promises that are *not* made! It's not guaranteed when the method
is called nor if it is called at all!

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.