On 4/27/07, C.  Handel <[EMAIL PROTECTED]> wrote:
> I have troubles with the Garbage Collection and Cleanup ob some
> Objects.
>
> class Crawlerlog will open a file for appending.
> class Crawler will create an instance of crawlerlog
> controller Main will create an instance of Crawler and start it in a
> new thread.
>
> The Thread will finish, but neither Crawler.__del__ nor
> Crawlerlog.__del__ are called.
>
> Some pseudocode
>
> def thread(crawler):
>    try:
>      crawler.work()
>    finally:
>      print "finished"
>      del crawler
>
> class Crawlerlog(object).
>     def __init__(self):
>        self.file = open("somefile", "a+")
>     def __del__(self):
>        close(self.file)
>
> class Crawler(object):
>    def __init__(self):
>        self.log = Crawlerlog()
>    def __del__(self):
>       print "bye"
>
> def Main(BaseController):
>     def index(self):
>       crawler = Crawler()
>        t = Thread(target=thread, args=(crawler) )
>        t.start()
>       return Response("started")

I don't know the answer 100%, but I do know that:

a) If you have cyclical references in Python, the garbage collector
will never break the references if any of the objects have a __del__
method.

b) You may be able to solve your need to close the file object by
using the atexit module.

c) There may be even more ways to do this such as by explicitly
closing things up without using a __del__.

Happy Hacking!
-jj

-- 
http://jjinux.blogspot.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to