我正在Windows 10下使用安装了最新LibXML(2.0134)的perl 5.24.4。下面是在Windows 10上崩溃的代码(它在Linux上有效)
use strict;
use warnings;
use XML::LibXML;
use Utils;
use Log::Log4perl;
Log::Log4perl->init('logger.conf');
my $logger = Log::Log4perl->get_logger();
sub generate_word_stats_for_chunk
{
return Utils::load_sql();
}
my $num_process = 4;
my @modes = (1,3,5);
foreach my $mode (@modes) {
my @pid_list;
$logger->info("Forking $num_process");
for my $i (0 .. $num_process - 1) {
my $pid = fork();
$logger->logconfess("Unable to fork $!") if not defined $pid;
if ($pid == 0) {
# child process
generate_word_stats_for_chunk($i);
sleep 2;
# make sure that the child process exit
exit;
}
# parent
# Store the PIDs, so that waitpid() can call them later
push @pid_list, $pid;
}
for my $pid (@pid_list) {
waitpid $pid, 0;
}
$logger->info("finished mode $mode");
package Utils;
use strict;
use warnings;
use XML::LibXML;
use Log::Log4perl;
my $logger = Log::Log4perl->get_logger();
sub load_sql
{
$logger->info("parsing XML");
my $dom;
eval {
$dom = XML::LibXML->load_xml(location => 'sql.xml');
};
if ($@) {
$logger->error("parse error $@");
return;
}
$logger->info("finished parsing XML");
# dummy
return {};
}
# perl package ends with 1
1;
<?xml version="1.0" encoding="UTF-8"?>
<sqls>
<sql name="CREATE_COMMODITY_META">
test
</sql>
</sqls>
运行crash.pl给我以下输出
INFO main::(crash.pl:37) Forking 4
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO main::(crash.pl:62) finished mode 1
INFO main::(crash.pl:37) Forking 4
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
如果我删除Utils::load_sql()
语句,该代码将起作用。该可能与Why does Perl crash when using LibXML after a fork?类似,但是我对此有所怀疑,因为该文章中的代码仅适用于机器。如果您删除外部循环(例如foreach my $mode...
),则我的代码实际上可以正常工作。我也怀疑该帖子已经有8年历史了。
此外,为什么Utils.pm
中的eval语句不能捕获崩溃?