in reply to Re^2: simple timeout
in thread simple timeout

This is untested code. I don't have a bunch of servers against which to test.

The basic idea is that you detch a thread that makes the connection to the server. You arrange a shared flag to be clear before the WMI attempt is made and set after.

The main thread then waits for the flag to be set or timeout. If timeout occurs, then it just leaves the thread hanging. It will get cleaned up when the process exits. This is not particularly elegant, but iThreads lacks a defined API for killing a thread.

I do have a mechanism that will do this by using the Win32API TerminateThread() which seems to work okay in my limited testing, but it could leave Perl in a confused state. Better I think to let any nasties happen when the process is going to exit anyway.

#! perl -slw use strict; use threads qw[ async ]; use threads::shared; use Win32::OLE qw (in); my @servers = qw[ servera serverb serverc ]; my $namespace="\\root\\cimv2"; for my $server ( @servers ) { my $gotObject : shared = 0; my $gotInfo : shared = 0; my %info : shared; async { my $object=Win32::OLE->GetObject( 'winmgmts:{impersonationLevel=impersonate,(security)}//' . $server . $namespace ) or die "could not get object"; $gotObject = 1; # NOW GET DRIVE INFO THROUGH WMI $info{ somekey } = 'somevalue'; $info{ someotherkey } = 'someothervalue'; ## Yada yada undef $object; ## Last thing before exiting thread. $gotInfo = 1; }->detach; my $timeout = 30; sleep 1 while $timeout-- and not $gotObject; unless( $timeout ) { print "Failed to make connection to $server"; next; } sleep 1 until $gotInfo; print "Received from $server"; print "$_ => $info{ $_ }" for sort keys %info; }

The next logical progression once your using threads would be to overlap the connections to all the servers and queue the results back to the main thread for final disposal. The reason I am not suggesting this is that some OLE objects are not designed for being called from multiple threads. Also, I have experienced some problems with using Win32::OLE from multiple concurrent threads.

Sorry it's not a tested solution. If it works, it would be most useful to those whom follow after you if you could post back a working copy of this test code with any corrections you have to make. Thanks.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon