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

Hi,

I am trying to integrate IPC::Shareable module to Parallel::Forkmanager module, but having troubles with global variables.

I have developed a proof of concept script for myself to see how IPC::Shareable is actually working.

use Parallel::ForkManager; use IPC::Shareable; my $pm = new Parallel::ForkManager(10); my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my $testvr = 0; tie $testvr, 'IPC::Shareable', 'data', \%options; for(my $i = 0; $i < 100; $i++) { $pm->start and next; (tied $testvr)->shlock; $testvr++; (tied $testvr)->shunlock; print $testvr . "\n"; $pm->finish; } $pm->wait_all_children;

This script works as intended, but when I try to modify $testvr variable inside for loop, it is not working correctly. What should I do to fix this issue? Any ideas?

Example:

for(my $i = 0; $i < 100; $i++) { $pm->start and next; if($testvr >= 5) { $testvr = 0; } (tied $testvr)->shlock; $testvr++; (tied $testvr)->shunlock; print $testvr . "\n"; $pm->finish; } $pm->wait_all_children;

The output should be:

1
2
3
4
5
1
2
3
4
5
...

but it is:

1
2
3
4
5
1
1
2
3
4
5
6

(locking variable inside if statement is not working)

I am sure I am missing something obvious but could not find solution.

Thanks in advance,

Replies are listed 'Best First'.
Re: IPC::Shareable and Parallel::Forkmanager Question
by spx2 (Deacon) on Nov 03, 2009 at 14:18 UTC

    Do you need to use IPC::Shareable and Parallel::Forkmanager ? Maybe you can use POE instead

      I have not worked on POE before, so if I can fix this code that would be better.

      I may have to learn POE if it is not possible with them, however.

      Thanks,

Re: IPC::Shareable and Parallel::Forkmanager Question
by BioLion (Curate) on Nov 03, 2009 at 21:09 UTC

    I can't test my theory ( no IPC::Shareable on this machine ), but possibly the problem is that although you shlock your variable for the increment, you don't for when you test and restart it... what happens when you change you code to this? :

    for(my $i = 0; $i < 100; $i++) { $pm->start and next; (tied $testvr)->shlock; if($testvr >= 5) { $testvr = 0; } $testvr++; print $testvr . "\n"; tied $testvr)->shunlock; $pm->finish; } $pm->wait_all_children;
    Just a something something...

      I came here to correct my code but you have corrected it already :)

      Yes, that's the problem. Although I have said in my first post that locking does not help, I have done it wrong.

      What I have tried was this:

      for(my $i = 0; $i < 100; $i++) { $pm->start and next; (tied $testvr)->shlock; if($testvr >= 5) { $testvr = 0; } $testvr++; (tied $testvr)->shunlock; (tied $testvr)->shlock; print $testvr . "\n"; tied $testvr)->shunlock; $pm->finish; } $pm->wait_all_children;

      It does not work correctly. I was locking and unlocking twice. As it was a testing code, I did not care to write locking statement twice, I assumed that it is doing the same thing.

      Thanks