in reply to Re^2: Shared memory and asynchronous access
in thread Shared memory and asynchronous access

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.

Replies are listed 'Best First'.
Re^4: Shared memory and asynchronous access
by Anonymous Monk on Apr 05, 2017 at 21:15 UTC

    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.

Re^4: Shared memory and asynchronous access
by vef445 (Novice) on Apr 05, 2017 at 19:36 UTC

    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 :)