Hi,
I want to use multi-thread to do parallel work. The code is like following:
$session = Vim->new(service_url => 'https://****/sdk');
$session->login(user_name => "***", password => "***");
for (my $i = 0; $i < 3; $i++) {
my $t = Thread->new(\&sdk_fun, $i);
push(@threads, $t);
}
foreach (@threads) {
$_->join; ############Error Here#############
}
The join() of the threads throws segmentation fault. Please Help.
Thanks in advance,
Shawn
The Perl modules within SDK for Perl 4.0 are not guaranteed to be threadsafe.
The only workaround is to use separate processes rather than separate threads.
Thanks, I got that.
Shawn
what happens if you $thr->detach; ?
I'm not having any trouble using threads with the vSphere Perl API, however Thread::Pool::Simple seeme to be borked
-
cut here -
#
test for concurrent VMware OPS
check for thread-safe operation
#
use warnings;
use strict;
use Data::Dumper;
use VMware::VIRuntime;
this is wierd, I had to add these additional modules to get it to compile
probably some wierdness in the ActiveState PDK automatic module detection
use Class::MethodMaker::scalar;
use VMware::VIM25Runtime;
use threads;
use Thread::Pool::Simple;
my $uuid;
my @vms = qw(
502233ed-c982-0405-0509-f5aeff1d4406
5022b475-e6aa-f800-0bf1-a31e5a931f0d
5022bc05-e556-86a4-55ea-b46e319f695c
50226925-847e-6066-d50d-70e4dc8f8769
50223c19-5c4b-00e5-3fd7-0ceb68c329ec
50226e7a-d96c-7a56-5856-083552fe5015
50220054-ca7f-cb7a-1d0a-4b58050de6d2
502263f0-3b9c-00a0-d724-7e36b78076dc
50221aa9-5b6e-6998-ef67-70d52955ac97
50224e7e-5d1b-6d37-2ab6-f6ff35a42763
);
sub find_snapshot_name {
my ($tree, $name) = @_;
my $ref = undef;
my $count = 0;
foreach my $node (@$tree) {
if ($node->name eq $name) {
$ref = $node;
$count++;
}
my ($subRef, $subCount) = find_snapshot_name($node->childSnapshotList, $name);
$count = $count + $subCount;
$ref = $subRef if ($subCount);
}
return ($ref, $count);
}
sub testFixlets {
my $uuid = shift;
print "Job # $uuid now running\n";
my $url = 'https://virtualcenter.bigfix.com:443/sdk';
my $user = 'my_username';
my $pass = 'my_password';
my $snapshot_name = 'Clean';
my $ipaddress;
connect to vmware
print "$uuid connecting to vmware\n";
my $sess = Util::connect($url, $user, $pass);
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', filter => {'summary.config.uuid' => $uuid});
print "$uuid done searching\n";
foreach my $vm (@$vm_views) {
get the snapshot object and power it on
my $host_obj = $vm->runtime->host;
my $hostname = Vim::get_view(mo_ref => $host_obj)->name;
my $ref = undef;
my $nRefs = 0;
if(defined $vm->snapshot) {
($ref, $nRefs) = find_snapshot_name ($vm->snapshot->rootSnapshotList, $snapshot_name);
} else {
print "There are no snapshots for this vm\n";
};
if (defined $ref && $nRefs == 1) {
my $snapshot = Vim::get_view (mo_ref =>$ref->snapshot);
print "$uuid reverting to snapshot: $snapshot_name\n";
$snapshot->RevertToSnapshot();
$vm->PowerOnVM();
$ipaddress = $vm->summary->guest->ipAddress;
}
}
sleep 15;
print "Job # $uuid returning\n";
return($uuid,$ipaddress);
}
#
#
THIS BREAKS PERL FOR SOME WIERD REASON
#
#my $no_threads = 3;
#my $pool = Thread::Pool::Simple->new(
min => $no_threads,
max => $no_threads,
#passid => 1,
load => 10000,
lifespan => 10000,
);
queue up the jobs
We can use this to keep track of how many we are running at tone time
my @running = threads->list(threads::running);
my @threads;
foreach my $uuid (@vms) {
#Thread::Pool::Simpler breaks VmWare vSphere ActiveState Perl
#my $id = $pool->add($uuid);
my ($thr) = threads->create(\&testFixlets,$uuid);
push @threads, $thr;
print "queued job number: for uuid: $uuid\n";
}
print "joining threads\n";
foreach my $t (@threads) {
my @r = $t->join();
print Dumper @r;
}
exit;
-
cut here -