sharing a (tied) hash between processes

Discussion in 'Perl Misc' started by Thomas Reat, Jan 1, 2004.

  1. Thomas Reat

    Thomas Reat Guest

    I have a perl script that maintains its state in a hash of hashes. It
    runs all the time. I have another script that must access (read only)
    this state. I'd also like to recover it in the event of a restart.

    What I'm doing now is to dump it to a file (200kB) every time through
    the main loop (every minute or so), despite the fact that the common
    case is for there to be no changes. And any changes that are made are
    tiny, to only a couple records. Locking is handled by writing to a
    temporary file and renaming over the original.

    What is an elegant solution for this? I've considered using a real
    database, but it seems like overkill. MLDBM looks like it might help
    me, but will I be able to have a dbm file opened for writing by one
    process and reading by another?
     
    Thomas Reat, Jan 1, 2004
    #1
    1. Advertising

  2. Thomas Reat

    Andy Baxter Guest

    At earth time Thu, 01 Jan 2004 12:49:07 -0800, the following transmission
    was received from the entity known as Thomas Reat:

    > I have a perl script that maintains its state in a hash of hashes. It
    > runs all the time. I have another script that must access (read only)
    > this state. I'd also like to recover it in the event of a restart.
    >
    > What I'm doing now is to dump it to a file (200kB) every time through
    > the main loop (every minute or so), despite the fact that the common
    > case is for there to be no changes. And any changes that are made are
    > tiny, to only a couple records. Locking is handled by writing to a
    > temporary file and renaming over the original.
    >
    > What is an elegant solution for this? I've considered using a real
    > database, but it seems like overkill. MLDBM looks like it might help
    > me, but will I be able to have a dbm file opened for writing by one
    > process and reading by another?


    The HTML::Template module I'm using can be set to use a module called
    IPC::SharedCache to store the parsed templates between calls, so you could
    have a look at that.

    andy.

    --
    http://www.niftybits.ukfsn.org/

    remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
    bin unless notified with
    HTML:
     or [attachment] in the subject line.
     
    Andy Baxter, Jan 1, 2004
    #2
    1. Advertising

  3. Thomas Reat

    R. KRause Guest

    (Thomas Reat) wrote in message news:<>...
    > I have a perl script that maintains its state in a hash of hashes. It
    > runs all the time. I have another script that must access (read only)
    > this state. I'd also like to recover it in the event of a restart.


    Hmm, this is a good question. I've been on the lookout for an elegant
    solution to this for quite some time. In one of the modules I wrote
    for a specialized chat daemon last year, I resorted to doing a process
    fork, and then passing my read-only hash data to a sister process via
    bidirectional interprocess pipes using an object request broker type
    methodology. The primary advantage was that neither process had to
    rely on disk storage operations, thus removing a slower middle layer,
    and even more importantly it alleviated dependancy on the internal
    data structures used by either process, thus providing a level of
    transparency. The disadvantage, was that request operations were not
    preemptive (they had to sit in a queue awaiting selection by the host
    process, like a typical client-server).

    There are other ways of accomplishing this bidirectional communication
    technique in UNIX without forked processes, such as open2() and tty
    device emulation -- and, if you really want to have fun, socket I/O.
    However, I don't believe there is any way to directly share namespaces
    or typeglobs between entirely disassociated perl processes. However, I
    would be eager to learn that I am wrong in that regard! :)

    --Randall
     
    R. KRause, Jan 2, 2004
    #3
  4. Thomas Reat

    Guest

    (Thomas Reat) wrote:
    > I have a perl script that maintains its state in a hash of hashes. It
    > runs all the time. I have another script that must access (read only)
    > this state. I'd also like to recover it in the event of a restart.
    >
    > What I'm doing now is to dump it to a file (200kB) every time through
    > the main loop (every minute or so), despite the fact that the common
    > case is for there to be no changes. And any changes that are made are
    > tiny, to only a couple records. Locking is handled by writing to a
    > temporary file and renaming over the original.
    >
    > What is an elegant solution for this? I've considered using a real
    > database, but it seems like overkill.


    I would argue that MySQL is perhaps not overkill. (If in the right mood, I
    may also argue it isn't a real database).

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
     
    , Jan 2, 2004
    #4
  5. Thomas Reat

    pkent Guest

    In article <20040102154252.513$>,
    wrote:

    > (Thomas Reat) wrote:
    > > I have a perl script that maintains its state in a hash of hashes. It
    > > runs all the time. I have another script that must access (read only)
    > > this state. I'd also like to recover it in the event of a restart.
    > >
    > > What I'm doing now is to dump it to a file (200kB) every time through
    > > the main loop (every minute or so), despite the fact that the common
    > > case is for there to be no changes. And any changes that are made are
    > > tiny, to only a couple records. Locking is handled by writing to a
    > > temporary file and renaming over the original.
    > >
    > > What is an elegant solution for this? I've considered using a real
    > > database, but it seems like overkill.

    >
    > I would argue that MySQL is perhaps not overkill. (If in the right mood, I
    > may also argue it isn't a real database).


    I'd second that - it's a database that I'm reliably informed is very
    good in read-heavy operations, which sounds like the case here: usually
    the state doesn't change, which means there's little writing going on
    but much reading.

    [ OTOH I have ranted to my coworker at discovering that our version[1]
    of MySQL lacks certain features that I take for granted in Oracle, and
    indeed that one clause was silently ignored... ]

    Also, in my place of work we use MySQL for storing this kind of 'state
    information that can be used by more than one program at a time' in all
    sorts of applications and it seems to work really well.

    Obviously if you've already got another database available then I'm not
    saying you _have_ to use MySQL, just that it would probably be suitable
    for your application.

    P

    [1] Your version may differ, read the fine manual, etc

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
     
    pkent, Jan 3, 2004
    #5
    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. rp
    Replies:
    1
    Views:
    591
    red floyd
    Nov 10, 2011
  2. Dan
    Replies:
    1
    Views:
    105
    Ben Morrow
    Nov 21, 2003
  3. Dan Anderson

    What is a tied hash?

    Dan Anderson, Nov 21, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    318
    Tad McClellan
    Nov 21, 2003
  4. Replies:
    14
    Views:
    271
    Tomi Häsä
    Jan 10, 2005
  5. bernd
    Replies:
    0
    Views:
    649
    bernd
    Apr 24, 2012
Loading...

Share This Page