perl多线程实例(四个线程同时读MYSQL数据库获取记录)

来源:互联网 发布:java物联网开发技术 编辑:程序博客网 时间:2024/04/20 02:15
use threads;use DBI();# input:# $_[0]: fruitnamesub 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();}


原创粉丝点击