On Tue, 15 Jul 2003 17:35:56 -0700, Jamin Roth wrote:
>I'm having some trouble getting DBI and multiple forks to function.
>I have goten it to work if I only spawn one child using static
>variables.
>But using arrays of children hasn't worked.  I got some code similar
>from: http://gsu.linux.org.tr/oreilly/perl/cookbook/ch17_13.htm.
>Here is my current attempt:
>
[ snip]

>$dbh = DBI->connect($dsn,$user,$password);

Rule #1 - Do Not Share A Parent-Created Connection In Multiple Children. Think about
it - you have five children writing to the same connection. Unless
the database client library code is threaded, this is bound to wreak
havoc.

In general, the rule should read Do Not Share A Parent-Creted RESOURCE In Multiple 
Children. See NEW CODE below:

sub make_new_child {
        my $pid;
        my $sigset;

        $pid = open(KID, "-|");
        $|=1;
        if ($pid) { # Parent records the child's birth and returns.
                $children{$pid} = 1;
                $children++;
                $FuncCount++;
                return;
        } else { # Child can *not* return from this subroutine.
                $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before

                ## NEW CODE:
                ## Do the connect here in each child...
                my $dbh = DBI->connect($dsn,$user,$password);
                ## EONEW CODE

                my $sth = $dbh->prepare($Funcs[$FuncCount]);
                $sth->execute();
                my $row = $sth->fetchrow_arrayref;
                print $$row[0];

## etc....
--
Matthew O. Persico



Reply via email to