use threads;
use DBI();
# input:
# $_[0]: fruitname
sub fetchFruit {
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=FruitDB;host=localhost",
"username", "password",
{'RaiseError' => 1});
# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM fruit where name = '".$_[0]."'");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: name = $ref->{'name'}, variety = $ref->{'variety'}\n";
}
$sth->finish();
# Disconnect from the database.
$dbh->disconnect();
}
my @arr = qw(APPLE ORANGE PEAR BANANA);
my @threads;
foreach (@arr) {
push @threads, threads->new(\&fetchFruit, $_);
}
foreach (@threads) {
$_->join();
}
perl多线程实例(四个线程同时读MYSQL数据库获取记录)
最新推荐文章于 2024-06-23 14:45:43 发布