http://www.perlmonks.org?node_id=1014866

Fré has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

While querying churches sequentially, took literally hours, I've been trying to query them in parallel.
Every fork is supposed to generate a file-per-church.
Whereafter each file will be loaded and stored in a major hash, which would be parsed using Win32::OLE to generate an excel-doc.

Yet, it all seems to work until I load the Win32::-modules. Forking (still learning heaps about that thing) works rather the way I want it, but when I try to merge the fork-code with excel-part, then waitpid never returns.
Forking goes up until exit, but waitpid seems to wait forever.
I'm not sure how to interpret filehandles during forks, so wondering whether something remains open and locking the fork.

I'm bound to use ActiveState Perl 5.6(.1?).

Here's how I tried it.

snipsnip...
use warnings; use strict; #use Win32::OLE; #use Win32::OLE qw(in with); #use Win32::OLE::Variant; #use Win32::OLE::Const 'Microsoft Excel'; use POSIX; use Time::Local; use IPC::Open3; use File::Copy; use File::stat; use Getopt::Std; use Time::Local; use Data::Dumper; my $EXCEL; $SIG{__DIE__} = sub { $EXCEL->Quit() if (defined($EXCEL)); print "Died +: "; CORE::die @_ }; $SIG{__WARN__} = sub { die "Warning-SIG caught: ".$_[0] }; select STDERR; $|=1; select STDOUT; $|=1; my $starttime=localtime(); print "------ $starttime ---------------"; my @BLIST=("floep","flap"); my %proclist; foreach my $boxnr (@BLIST) { $proclist{$boxnr}=fork(); if ($proclist{$boxnr}== 0) { my $rc=0; print "Starting child ($$) for box $boxnr."; $rc=system("qu +ery.pl $boxnr"); print "Succesfull Query (fork: $rc=>$?) for box $boxnr...\n"; exit 0; } else { print "Child generated: PPID: $$ - Fork: $proclist{$boxnr}\n +"; } } foreach my $proc (keys(%proclist)) { print "Waiting to end... "; my $porki=waitpid($proclist{$proc},0); print "Porki: $porki - $proc\n"; } foreach my $file (@files) { local $/=""; my $boxnr=substr($file,0,index($file,"_")); print "($boxnr) File: ".$file."\n"; open FH,"<$file" or die "File-issue: $file\n"; my $tempstring=<FH>; close FH; my %hash=%{eval "$tempstring"}; $BOX{$boxnr}=$hash{$boxnr}; }
... snip...

Replies are listed 'Best First'.
Re: Fork vs Win32::OLE
by nikosv (Deacon) on Jan 23, 2013 at 13:05 UTC

    Forking in Windows is simulated by threads underneath. Win32::OLE is not thread safe plus the Excel automation object lives in the STA so the call must be serialized

    From : Threading Support in Office

    "The Office object model is not thread safe, but it is possible to work with multiple threads in an Office solution. Office applications are Component Object Model (COM) servers. COM allows clients to call COM servers on arbitrary threads. For COM servers that are not thread safe, COM provides a mechanism to serialize concurrent calls so that only one logical thread executes on the server at any time. This mechanism is known as the single-threaded apartment (STA) model. Because calls are serialized, callers might be blocked for periods of time while the server is busy or is handling other calls on a background thread. "