Hello Everyong,
    I have read and heard a few times that
multi-threading is not supported in DBI.  I have a
script that sends thousands of emails on a Windows. 
Of course...I can't send thousands of messages in a
single process, so I had a great idea, lets calculate
the number of emails that we are going to send and
divide it by a number of emails per thread.  For
example, we are going to send 21,000 email and we want
each thread to handle 1000 messages...so we are going
to spawn 21 threads.  Each thread is then spawned and
makes a query to the database so each thread gets it
1000 emails, so thread number three will go get emails
3000 - 3999.  Is something like this possible?  My
script is below for reference.

Thanks -- Ken

use strict;
use DBI;

$| = 1;

##############################################################
# Setup the constants for the mailer sConnection,
sUsername, #
# sPassword                                           
      # 
##############################################################

our $sConnection = "dbi:ODBC:Staging";
our $sUserName   = "KSwift";
our $sPassword   = "is.locked";
our $iThreadSize = "20";
our $sDatabase;
our $iUserCount;
our @aRecordSet;

#################################
# Connect to the database       #
#################################
    $sDatabase = DBI->connect( $sConnection,
$sUserName, $sPassword) || die "Unable to connect: " .
$DBI::errstr . "\n"; 

###############################################
# Prepare count of rows and execute the query #
###############################################
   my $sQuery = $sDatabase->prepare( "SELECT count(*)
as UserCount from [ePassportTest]"); 
   $sQuery->execute(); 
   {
      $iUserCount = $sQuery->fetchrow_array();
      $sQuery->finish;
   } 

#################################
# Close the database connection #
#################################

$sDatabase->disconnect;

#####################################################
# Divide the returned user count by the thread size #
# and set the threadcount.                          #
#####################################################

our $iThreadCount = $iUserCount / $iThreadSize;

#####################################################
# If the number of threads is not a whole number    #
# ie...it has a decimal. Then round up and remove   #
# the decimal value.                                #
#####################################################

if ($iThreadCount =~ m/\.(.+)/i) {
   $iThreadCount = $iThreadCount + 1;
   $iThreadCount = substr($iThreadCount, 0,
index($iThreadCount, "."));
}


#######################################################
# Create a thread from 1 to the value of iThreadCount
#
#######################################################


my %iChildpid;
my $iItem;

for ($iItem=1; $iItem<=$iThreadCount;$iItem++) {
   $iChildpid{$iItem} = fork ();
   die "fork failed: $!\n" if not defined
$iChildpid{$iItem};

   if ($iChildpid{$iItem} == 0) {
      my $sEmailDB;
      my $sEmailQuery;

      #################################
      # Connect to the database       #
      #################################

      $sEmailDB = DBI->connect( $sConnection,
$sUserName, $sPassword) || die "Unable to connect: " .
$DBI::errstr . "\n"; 

      ###############################################
      # Prepare count of rows and execute the query #
      ###############################################

#      $sEmailQuery =
$sEmailDB->prepare('ePassportEmails
$iChildpid{$iItem}, $iThreadSize');
#      $sEmailQuery->execute(); 
#      {
#         while (my @aEmailData =
$sEmailQuery->fetchrow_array) {
#            my $iUserID = $aEmailData[1];
#            my $sEmail  = $aEmailData[2];
#            print "$iUserID $sEmail\n";
#         }
#      }

      #################################
      # Close the database connection #
      #################################

      $sEmailDB->disconnect;

      #################################
      # Sleep for a second, then the  #
      # next thread will startup      #
      #################################

      sleep(1);

      #################################
      # Exit this thread after all    #
      # the emails have been sent.    #
      #################################

      exit;

   } else {
      print "Parent: created PID
$iChildpid{$iItem}\n";
   }
   sleep(1);
}

exit 0;

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

Reply via email to