There are a few postings about DBI handles and forking, but I seem to be having an odd problem (the best kind :) The general tone of using DBI and fork, is to create your dbi object after forking and then all is well... However I'm having some issues. First off, I'm using active perl but I hope that isn't the problem. Anyway, here is some code...
sub spider { my $pid=fork(); if ($pid) { print "Parent.. returning\n"; return; } use DBI; my $sdbh = DBI->connect( "DBI:mysql:dbase;", 'username','password' ) or die "Couldn't connect to database: " . DBI->errstr; print "spider post-fork\n"; my $string = "SELECT data FROM info WHERE name='spiders'"; my $sth = $sdbh->prepare($string); #$sth->{InactiveDestroy}=1; $sth->execute; my $data = $sth->fetchrow_arrayref(); my $p=$data->[0]; print "spider says P is $p\n"; for (my $i=1;$i<10;$i++) { print "forking for ftpspider\n"; $pid=fork(); if (!$pid) { print "child... calling ftpspider\n"; ftpspider($i); } print "parent... finishing loop\n"; $|++; } print "finishing up db connections\n"; $sth->finish; $sdbh->disconnect; } sub ftpspider { my $call=shift; $|++; print "ftpspider called with $call... we're the grandchild\n"; use DBI; print "DBI used ok\n"; my $gdbh = DBI->connect( "DBI:mysql:dbase;", 'username','password' ) or die "Couldn't connect to database: " . DBI->errstr; print "gdbh created ok\n"; $gdbh->do("UPDATE info SET data=data+1 WHERE name='spiders'"); print "updated ok\n"; print "done updating grandchild... disconnecting\n"; $gdbh->disconnect; print "grandchild done.... exiting\n"; exit; }
Although this code doesn't do much, it tends to hang at the second fork (forking for ftp spider) I'm not aware of any limitation of grandchild forking... My only guess right now is that the grandchild DBI in ftpspider is conflicting with the child DBI in spider somehow... (BTW, DBI is also needed in the parent). Anyone have any similar experiences with using multi-generational DBI's? FWIW, I've also tried moving one of the forks to the ftpspider function. I've also tried making a dbi handle in the spider function and passing it to the ftpspider after it forks. No luck either way. As far as error messages go, I've been getting the following:
dbih_clearcom (h 0x3659508, com 0x571e828): FLAGS 0x111: COMSET Warn PrintError TYPE 2 PARENT DBI::dr=HASH(0x3659538) KIDS 1 (1 Active) IMP_DATA HASH(0x3659448) in 'DBD::mysql::db' Attempt to free non-existent shared string during global destruction.
help :]

In reply to DBI and fork() by Anonymous Monk

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.