#! perl -slw use strict; use threads; use threads::shared; use DBI; ##### Warning: entirely untested code ############## our $THREADS ||= 5; ## A shared 'semaphore' ## This works fine in single cpu boxes ## but may need to be replaced with a 'proper' semaphore ## on multi-cpu machines my $running : shared = 0; sub worker { $running++; ## get a new handle to the DB in each thread my $dbh = DBI->connect(...); my $sth = $dbh->prepare( "INSERT INTO T_$filename (" . join( ', ', @column ) . ") VALUES (" . join( ',', ( '?' ) x @column ) .")" ); ## get a handle to the CSV file for this thread my $fileno = shift; open my $fh, "<&=$fileno" or die $!; ## process the data ## batching would be of little benefit ## if you're going to read a batch ## and then execute them individually while( <$fh> ) { $sth->execute( split ',', $_ ); } $dbh->disconnect; $running--; } my $fh; ## Open the file in root open $fh, 'file.csv' or die $!; ## and then 'share' it with the worker threads by passing the fileno threads->create( \&worker, fileno( $fh ) )->detach for 1 .. $THREADS; sleep 1 while $running < $THREADS; ## Let'em start sleep 1 while $running; ## Wait for'em to finish