in reply to A per-instance shared queue among forked child processes?

I can't easily use normal (permanent) tables because multiple instances of the script will step all over each other.

Why not just use a permanent table that is associated with each child's process id?

my $pid = fork(); if (not defined $pid) { warn "resources not avilable.\n"; } elsif ($pid == 0) { print "Child: My PID is $$\n"; exit(0); } print "Parent: I talk to child by adding to QUEUE associated with $pid +\n";

Replies are listed 'Best First'.
Re^2: A per-instance shared queue among forked child processes?
by EvanK (Chaplain) on Apr 04, 2011 at 19:57 UTC
    That's a great idea that I can't believe I didn't think of. I can create the table in the parent:
    $queue_table = 'queue_'.$$; $dbh->do('CREATE TABLE '.$queue_table.' (payload TEXT NOT NULL)');
    And then access it from the children after the fork, via $queue_table.
      I personally would not create a new table for each instance, but would instead have a static table that contains the key PARENTID or CHILDID depending on if you wanted the queues to be associated to the parent or child.
      my $sth = $dbh->prepare(q{INSERT INTO TABLE QUEUE SET PARENTID=?, PAYL +OAD=?}); $sth->execute($parentid, $payload) or die $dbh->errstr;

      Before each new instance of a parent is run, it should simply scan the table to see if there are any old records left over from a previous process with the same pid.