Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Fork vs Win32::OLE

by Fré (Initiate)
on Jan 23, 2013 at 09:14 UTC ( #1014866=perlquestion: print w/ replies, xml ) Need Help??
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.

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 $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...

Comment on Fork vs Win32::OLE
Download Code
Re: Fork vs Win32::OLE
by nikosv (Hermit) 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. "

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014866]
Approved by vinoth.ree
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-07-28 15:26 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (201 votes), past polls