我使用這篇文章:http://www.perlmonks.org/?node_id=594175來編寫代碼,將DBI和fork結合在一起。它適用於Linux,但不適用於Windows XP。我使用活動狀態Perl v5.10.0 MSWin32-x86-多線程,DBD :: mysql v4.011。DBI和fork不能在Windows上工作
在Linux上Perl v5.16.1 i486-linux-thread-multi DBD :: mysql v4.021。
代碼。 dbi_fork.pl:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
require "mysql.pl";
my $dbh = connect_mysql();
if (fork()) {
$dbh->do("UPDATE articles SET title='parent' WHERE id=1");
}
else {
my $dbh_child = $dbh->clone();
$dbh->{InactiveDestroy} = 1;
undef $dbh;
$dbh_child->do("UPDATE articles SET title='child' WHERE id=2");
}
mysql.pl:
sub connect_mysql
{
my $user_db = 'user';
my $password_db = 'secret';
my $base_name = 'test';
my $mysql_host_url = 'localhost';
my $dsn = "DBI:mysql:$base_name:$mysql_host_url";
my $dbh = DBI->connect($dsn, $user_db, $password_db) or die $DBI::errstr;
return $dbh;
}
1;
文章表:
DROP TABLE IF EXISTS `articles`;
CREATE TABLE `articles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of articles
-- ----------------------------
INSERT INTO `articles` VALUES ('1', 'title1');
INSERT INTO `articles` VALUES ('2', 'title2');
在Windows上,給出了一個錯誤:
$ perl ./dbi_fork.pl
DBD::mysql::db clone failed: handle 2 is owned by thread 2344b4 not current
thread 1a45014 (handles can't be shared between threads and your driver may
need a CLONE method added) at ./dbi_fork.pl line 14.
如何解決?
**(1)**你不能比較的行爲Windows版本與Linux版本;這裏有太多的變數。您可以通過在XP機器上安裝perl5.16(例如[Strawberry Perl](http://strawberryperl.com/))並安裝相同版本的DBD :: mysql來減少變量數量。 **(2)**無論如何,Windows沒有原生的'fork'。相反,它是使用線程來模擬的。看起來問題可以通過在叉子前克隆手柄並讓每個側面關閉未使用的手柄來解決。或者孩子重新運行'connect_mysql'來獲得第二個句柄。 – amon
@amon:您應該將其作爲答案發布。 – Borodin