gpt4 book ai didi

Perl Parallel::ForkManager 与 DBI 数据库处理程序

转载 作者:行者123 更新时间:2023-12-04 14:20:02 25 4
gpt4 key购买 nike

我对并行执行多个数据库操作感兴趣。我玩过 Perl Parallel::ForkManager 但还没有将它用于任何数据库。我读过数据库连接是 not supported very well有了这个。有任何人对此有经验吗?

作为一个例子,我可能会产生一个系统调用(它执行 DBI 工作)而不是原始代码,即

#!/opt/local/bin/perl -w

use strict;
use Parallel::ForkManager;

$| = 1;

my $max_procs = 10;

my $pm = new Parallel::ForkManager($max_procs);

for (my $var = 0; $var <= 10; $var++) {
my $pid = $pm->start('proc'.$var) and next;
sleep ( 2 );
system( "./DBworker.pl $var" );
$pm->finish(0);
}

print "Waiting for child procs\n";
$pm->wait_all_children;
print "complete!\n";

最佳答案

如果工作正在由其他程序完成,则 fork 没有危险。 ing。当您打开与数据库的连接然后 fork 时,危险就来了。 child 不能重用 parent 的连接;但是,看看 DBIx::Connector ,它会处理您在 fork 之后需要做的事情为您设置并运行多个程序通常不是正确的答案。

#!/usr/bin/perl

use strict;
use warnings;

use DBIx::Connector;
use Parallel::ForkManager;

my $dsn = "dbi:SQLite:dbname=foo.db";
my $user = "";
my $pass = "";
my $conn = DBIx::Connector->new($dsn, $user, $pass,
{
AutoCommit => 0,
PrintError => 0,
RaiseError => 1,
ChopBlanks => 1,
FetchHashKeyName => 'NAME_lc',
}
);
END { unlink "foo.db" }

#setup table
$conn->run(fixup => sub {
my $dbh = $_;
$dbh->do("create table foo ( id integer, name char(35) )");
my $sth = $dbh->prepare("insert into foo (id, name) values (?, ?)");
while (<DATA>) {
chomp;
$sth->execute(split /,/);
}
});

my $pm = Parallel::ForkManager->new(3);

my $sth = $conn->dbh->prepare("select * from foo where id = ?");
for my $id (1 .. 3) {
next if $pm->start;
$sth->execute($id);
while (my $row = $sth->fetchrow_hashref) {
print "$id saw $row->{id} => $row->{name}\n";
}
$pm->finish;
}

$pm->wait_all_children;

print "done\n";

__DATA__
1,foo
2,bar
3,baz

关于Perl Parallel::ForkManager 与 DBI 数据库处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com