Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

forks.pm, CGI::App,TT and DBD::mysql

by perldragon80 (Sexton)
on Jul 30, 2004 at 00:26 UTC ( [id://378561]=perlquestion: print w/replies, xml ) Need Help??

perldragon80 has asked for the wisdom of the Perl Monks concerning the following question:

I am having a forks.pm problem with a webapp that is using CGI::Application, Template Toolkit, and the mysql DBI modules.
My objective is to have several forked (could switch to threaded but trying not to) telnet sessions that go out to different routers, gather stat information, return all info and then output the info in a table(as well as save it to DB). If I use the forks module, I notice I am getting errors (on Apache) like:


1)DBI handle 0x8315140 cleared whilst still active
2)dbih_clearcom (drh 0x8315140, com 0x883e370, imp DBD::mysql::dr)
3)FLAGS 0x100215: COMSET Active Warn PrintWarn AutoCommit
4)PARENT undef
5)KIDS 0 (0 Active)
6)IMP_DATA undef

That is just for areas that deal with DB interaction. I also see errors when I attempt to open a filehandle: (Apache log shows in order)

1)cat, 2)write error, 3)broken pipe
##############################################
my $pid = open(CLI, "$ENV{HOME}/bin/cli all -D |") or die "couldn't fork: $!\n";

while(<CLI>) {
if ($_ =~ /\w+.*/) {
$output .= $_;
} else {
#do nothing
}
}
close(CLI);
###################################################
If I don't use forks.pm and switch to threads this doesn't happen, any ideas?


Thank you!

Replies are listed 'Best First'.
Re: forks.pm, CGI::App,TT and DBD::mysql
by gellyfish (Monsignor) on Jul 30, 2004 at 10:50 UTC

    The problems you are seeing when you are forking are because you are getting a copy of your DB handle in your child processes this is going to cause problems when they go out of scope or whatever. Either disconnect before you fork or connect after so this doesn't happen.

    /J\

      Thanks for the info about the DBH and forks. I got some of the errors to go away by closing the conn before the fork. I still have the same error with FileHandles and forks.pm. I decided to simplify the issue by creating a single script with no other modules and still saw the error with this one little script. This works just fine as soon as I comment out forks

      #!/usr/bin/perl -w
      # forks_test.pl
      use forks;

      my $pid = open(CLI, "$ENV{HOME}/bin/cli all -D |") or die "couldn't fork: $!\n";
      my $output = '';
      while(<CLI>) {
      #chomp($_);
      if ($_ =~ /\w+.*/) {
      $output .= $_;
      print "$_\n";
      } else {
      }
      }
      close(CLI);

      [prompt> ./forks_test.pl
      cat: write error: Broken pipe

      Any ideas out there about this one? I am using perl 5.8.5 on a RH9 linux box.

        This works just fine as soon as I comment out forks.

        Then leave use forks; commented out. Problem solved.

        That may sound like sarcasm, but it's also good advice. You do not need forks.pm in order to use fork or the forking open.

        Read the documentation on forks and you'll discover that it is a way of emulating the threads api. for those situations where threads are (or were) unreliable.

        For what you are doing in this snippet, you do not need and should not be using forks.

        So stop forking around and stop trying to use the module for things that it was not intended for :)

        You might also try reading http://perlmonks.com/index.pl?node=Writeup%20Formatting%20Tips


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://378561]
Approved by rlb3
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-07-12 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.