Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-09-2003, 12:04 AM   #1 (permalink)
Talleyrand
Registered User
 
Join Date: Mar 2003
Location: Seattle, WA
Posts: 7
Talleyrand is on a distinguished road
Send a message via ICQ to Talleyrand
PERL, mySQL, and fork() ?

Hey,

I'm wondering if someone can help me with some irregularities I'm encountering.

I have a script which acts as a daemon, which calls get_pending_records() to poll a mySQL DB for records where the 'processed' field = 'N' (for No). If there aren't any records that have been written, I sleep for a few moments and try again.

However, if there are records, they are returned to me in a passed-in array reference that is loaded with anonymous hash references, each hash reference being a returned record.

So I do something like this:

Code:
while ( 1 )
{
  my @records = ();
  get_pending_record( \@records );

  foreach my $record ( @records )
  {
    my $locked = lock_record( $record );
    if ( ! $locked )
    {
       ## Throw some error;
       next;
    }
	
    my $pid = fork();

    if ( ! defined $pid )
    {
      ## Throw some error
    }
    elsif ( $pid == 0 )
    {
      process_record( $record );
      exit;
    }
    else
    {
      print "Message from parent: process $pid handling record $record->{'record_id'}\n";
    }
  }

  sleep $sleep;
}
The lock_record function writes to the DB, updating the record to set the processed field = 'P' (for Processing). This should prevent the record from being retrieved a second time in the main outer loop while the process_record() function performs potentially lengthy operations.

Now the problem is this: When the script starts up and fetches the first batch of records for processing, I get some mySQL write failure errors--they always say "Lost connection while performing query". My database handle still contains an active connection, though.
It gets stranger. This is frequently the printout, for say three records that it wants to process:

[SHELL]
Message from parent: process 12345 handling record 1
Message from parent: process 12346 handling record 1
Message from parent: process 12347 handling record 2
Message from parent: process 12348 handling record 1
Message from parent: process 12349 handling record 3
[/SHELL]

Sometimes it's sane, and there are only three processes forked, one for each unique record.

The above code seems straightforward to me -- I can't figure out why three different children might be receiving the same hashref or why mySQL would be wigging out. I'm running this on a BSDi machine capable of handling ~1000 mySQL questions/minute -- the most I topped out at was 85 during testing. Would the DBI module handling the write during lock *return* a success before it was actually done writing? If that were the case, surely the sleep would take care of that and it would be finished by the time the main loop started over and get_pending_records() was called again.

Thirdly and finally, when I test this script extensively, the free memory on the server drops significantly--almost like there's a memory leak. fork() is a scary beast, but I'm exiting each child (or so I think).

Any help from the PERL/mySQL/UNIX gurus out there?

Thanks!
Talleyrand is offline   Reply With Quote
Old 04-09-2003, 09:09 PM   #2 (permalink)
Talleyrand
Registered User
 
Join Date: Mar 2003
Location: Seattle, WA
Posts: 7
Talleyrand is on a distinguished road
Send a message via ICQ to Talleyrand
I figured it out... thanks for all the help

A globally scoped database connection handle is not a good thing to be sharing amongst children.

Talleyrand
Talleyrand is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting