I'm writing a script that will fork off a series of child processes (let's say 5 or 10 as an example), and then all of the child processes will be adding and removing items for processing to/from a shared queue. The shared queue among the child processes is turning out to be kind of difficult to implement.

This queue really just needs to be a FIFO, where I can push items to the end of it, and pop items from the beginning of it. And it could get quite large, so I can't store it in-memory, because then I'll likely run afoul of the OOM killer. The real catch here is that each instance of the script will need its *OWN* shared queue, available only to its own child processes, because I'll likely end up running multiple instances of it at once.

I had thought of making a db connection in the parent process before forking, then cloning the db handle for each child and using a mysql temp table for the "queue", but it seems cloning actually makes a brand new user session with mysql, so one child can't use (or even see) another child's temp tables (as mysql by design restricts access of a temp table to the session in which it was created). I can't easily use normal (permanent) tables because multiple instances of the script will step all over each other.

So, how could I cleanly and efficiently share what could potentially be a LARGE amount of data between multiple child processes, yet keep it confined to only that instance of the script?

Update:

I ended up using chrestomanci's solution, with just one table and using the pid of the parent as a key for each instance. This seemed like the best of both worlds (easy to implement, easy to understand), and since it's a mysql instance on the same server, it's producing some good throughput.

I might look into SQLite as salva suggested, though I'm using a named lock for popping from the queue (since I have to do a select, followed immediately by a delete) and I'm foggy on how (or if) i can exclusively lock a sqlite db in the same fashion.

Update 2:

It looks like BEGIN EXCLUSIVE TRANSACTION should do essentially the same thing as I was using a named lock for, explicitly preventing other processes from reading/writing until the transaction is ended.


In reply to A per-instance shared queue among forked child processes? by EvanK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.