Cleanup when a object dies

Discussion in 'Python' started by Benjamin, Jan 23, 2008.

  1. Benjamin

    Benjamin Guest

    I writing writing a class to allow settings (options, preferences) to
    written file in a cross platform manner. I'm unsure how to go a about
    syncing the data to disk. Of course, it's horribly inefficient to
    write the data every time something changes a value, however I don't
    see how I can do it on deletion. I've read that __del__ methods should
    be avoided. So am I just going to have to force the client of my
    object to call sync when they're done?
     
    Benjamin, Jan 23, 2008
    #1
    1. Advertising

  2. On Jan 22, 7:54 pm, Benjamin <> wrote:
    > I writing writing a class to allow settings (options, preferences) to
    > written file in a cross platform manner. I'm unsure how to go a about
    > syncing the data to disk. Of course, it's horribly inefficient to
    > write the data every time something changes a value, however I don't
    > see how I can do it on deletion. I've read that __del__ methods should
    > be avoided. So am I just going to have to force the client of my
    > object to call sync when they're done?


    Lots of ways
    1. Try the atexit module
    2. Use a weakref callback
    3. Embed a client callback in a try/finally.
    4. Or, like you said, have the client call a sync() method -- this is
    explicit and gives the client control over when data is written.

    Raymond
     
    Raymond Hettinger, Jan 23, 2008
    #2
    1. Advertising

  3. Benjamin

    Benjamin Guest

    On Jan 22, 11:29 pm, Raymond Hettinger <> wrote:
    > On Jan 22, 7:54 pm, Benjamin <> wrote:
    >
    > > I writing writing a class to allow settings (options, preferences) to
    > > written file in a cross platform manner. I'm unsure how to go a about
    > > syncing the data to disk. Of course, it's horribly inefficient to
    > > write the data every time something changes a value, however I don't
    > > see how I can do it on deletion. I've read that __del__ methods should
    > > be avoided. So am I just going to have to force the client of my
    > > object to call sync when they're done?

    >
    > Lots of ways
    > 1. Try the atexit module
    > 2. Use a weakref callback

    So, I can keep a global list weakrefs to all the objects of my class
    that have been created. Then have the callback call sync on them?
    > 3. Embed a client callback in a try/finally.
    > 4. Or, like you said, have the client call a sync() method -- this is
    > explicit and gives the client control over when data is written.
    >
    > Raymond
     
    Benjamin, Jan 24, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr m?ll

    My session-object dies way to early

    Mr m?ll, Oct 12, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,902
    Mr m?ll
    Oct 19, 2004
  2. Tony  Lay
    Replies:
    1
    Views:
    909
    Tony Lay
    Jul 13, 2009
  3. ball

    object scope and cleanup

    ball, Apr 29, 2009, in forum: Ruby
    Replies:
    4
    Views:
    135
    Sebastian Hungerecker
    Apr 30, 2009
  4. Christopher M. Lusardi
    Replies:
    3
    Views:
    217
    Alan J. Flavell
    Oct 19, 2003
  5. Object cleanup

    , May 30, 2012, in forum: Python
    Replies:
    4
    Views:
    199
Loading...

Share This Page