in reply to Shared memory and asynchronous access

Hello Guillaume, welcome to the Monastery and to Perl, the One True Religion.

See MCE::Shared which works on Windows as well as Unix-like OSes, and is in my experience fast and powerful, so I would assume could handle your use case ( See MCE::Shared::Condvar ). Its author marioroy visits the Monastery frequently, which can make it easier to get help with the modules.

Hope this helps!


The way forward always starts with a minimal test.
  • Comment on Re: Shared memory and asynchronous access

Replies are listed 'Best First'.
Re^2: Shared memory and asynchronous access
by vef445 (Novice) on Apr 05, 2017 at 14:41 UTC

    Oh, thanks for the quick reply!
    I think I have tried that one few days ago without success, but my syntax may have been wrong (I'm really new to this and learning on my own). I've done some progress since :) so I'll try again.

    Edit
    I actually remember now: I have looked at this solution but I don't understand how variable are shared between scripts with MCE::Shared I don't see any ID or anything that would make the link and when I try it I have to declare the variable in both script (e.g. using $var = MCE::Shared->scalar; ) and both end up being totally independent.
    I might be missing something :(

      Generally, you would declare the shared variable in a script then fork a worker or workers to do the separate task, where they would have access to the shared var.

      Update: Here's a very simple demo:

      use strict; use warnings; use feature 'say'; use Time::HiRes qw/ time usleep /; use MCE::Shared; say sprintf '%-5s %-17s %-s', 'PID', 'Time', 'Action'; my $shared = MCE::Shared->condvar(0); say sprintf '%s %.06f Init val: %s', $$, time, $shared->get; my $pid = fork; if ( $pid ) { for ( 0 .. 19 ) { say sprintf '%s %.06f write: %s', $$, time, $shared->incr; usleep 10000; } } else { while (1) { usleep 50000; say sprintf '%s %.06f READ: %s', $$, time, $shared->get; } } __END__
      Output:
      $ perl shared.pl PID Time Action 16648 1491411785.712927 Init val: 0 16648 1491411785.713604 write: 1 16648 1491411785.724157 write: 2 16648 1491411785.734731 write: 3 16648 1491411785.745270 write: 4 16648 1491411785.755807 write: 5 16650 1491411785.763780 READ: 5 16648 1491411785.766309 write: 6 16648 1491411785.776740 write: 7 16648 1491411785.787272 write: 8 16648 1491411785.797800 write: 9 16648 1491411785.808273 write: 10 16650 1491411785.814272 READ: 10 16648 1491411785.818806 write: 11 16648 1491411785.829300 write: 12 16648 1491411785.839957 write: 13 16648 1491411785.850539 write: 14 16648 1491411785.861082 write: 15 16650 1491411785.864522 READ: 15 16648 1491411785.871574 write: 16 16648 1491411785.882031 write: 17 16648 1491411785.892612 write: 18 16648 1491411785.903151 write: 19 16648 1491411785.913714 write: 20 16650 1491411785.914756 READ: 20

      Hope this helps!


      The way forward always starts with a minimal test.

        1nickt++, thank you. Regarding MCE::Shared and when the condition var aspect is not needed, the shared scalar will do the same.

        my $shared = MCE::Shared->scalar(0);

        Workers fetch and increment accordingly.

        $shared->get; $shared->incr;

        As of this writing, MCE and MCE::Shared are at 1.827 and 1.823 respectively.

        Hi again 1nickt,
        Following your advise, I'm now attempting to transform the secondary script, compute.pl, as a single subroutine and make this script exporting the necessary variable and of course its subroutine. If I can achieve this by applying little modifications to it, then it solves my problem in what I believe to be an elegant way.

        Since this script was meant to be executed as "perl compute.pl -arg1 arg2 (...) arg10 input_file > output_file", I need to find a corresponding syntax for the file input and output when calling the subroutine.
        Exporting variable and subroutine from one script to another is ok, I know how to proceed :)

        Thanks again for guiding me in what looks to be the right direction :)

Re^2: Shared memory and asynchronous access
by shmem (Chancellor) on Apr 05, 2017 at 18:21 UTC
    Perl, the One True Religion.

    I almost downvoted you for that one, then I saw the smiley, albeit missing. ++ for the rest of your post ;-)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'