Hi fireblood,

First, a kind correction to perlfan's comment about MCE. All the methods in MCE and MCE::Shared that pertain to communication are IPC-based. That has been the case since version 1.0. There are examples out there where workers write data to /dev/shm or /tmp and pass the path via IPC to the manager process i.e. data being quite large.

I made a tiny change to your demonstration. MCE::Shared::Server handles requests transparently to and from the shared-manager process, where the data resides. This works as one would expect it to.

$ diff ex1.pl ex2.pl 2a3 > use MCE::Shared; 4c5 < my $string = "First value"; --- > tie my $string, "MCE::Shared", "First value";

That's a tiny glimpse of what MCE::Shared is capable.

OOP Usage

If you're having to fetch or store several thousands or millions of times, then OOP usage involves lesser overhead.

use strict; use warnings; use MCE::Shared; my $string = MCE::Shared->scalar("First value"); sub do_subroutine { print "I am the subroutine and my PID is $$\n"; $string->set("Second value"); } print "I am the invoking process and my PID is $$\n"; print "The value of \$string is ", $string->get(), "\n"; unless (fork) { do_subroutine (); exit; } my $waited_pid = wait; print "The PID of the child upon whom I waited was $waited_pid\n"; print "The value of \$string is ", $string->get(), "\n"; __END__ I am the invoking process and my PID is 22725 The value of $string is First value I am the subroutine and my PID is 22727 The PID of the child upon whom I waited was 22727 The value of $string is Second value

String Class demo 1

One is not forced to using various data modules that ship with MCE::Shared. Next is a demonstration using a custom string class. A shared object is created afterwards.

use strict; use warnings; use MCE::Shared; package My::String; sub new { my ($class, $string) = @_; return bless \$string, $class; } sub set { my ($self) = @_; $$self = $_[1]; return 1; } sub get { my ($self) = @_; return $$self; } package main; my $string = MCE::Shared->share( { module => "My::String" }, "First value" ); sub do_subroutine { print "I am the subroutine and my PID is $$\n"; $string->set("Second value"); } print "I am the invoking process and my PID is $$\n"; print "The value of \$string is ", $string->get(), "\n"; unless (fork) { do_subroutine (); exit; } my $waited_pid = wait; print "The PID of the child upon whom I waited was $waited_pid\n"; print "The value of \$string is ", $string->get(), "\n"; __END__ I am the invoking process and my PID is 22978 The value of $string is First value I am the subroutine and my PID is 22980 The PID of the child upon whom I waited was 22980 The value of $string is Second value

String Class demo 2

Here sharing is instantiated inside the string class.

use strict; use warnings; use MCE::Shared; package My::String; sub new { my ($class) = @_; my $string = MCE::Shared->scalar($_[1]); return bless [ $string ], $class; } sub set { my ($self) = @_; $self->[0]->set($_[1]); return 1; } sub get { my ($self) = @_; return $self->[0]->get(); } package main; my $string = My::String->new("First value"); sub do_subroutine { print "I am the subroutine and my PID is $$\n"; $string->set("Second value"); } print "I am the invoking process and my PID is $$\n"; print "The value of \$string is ", $string->get(), "\n"; unless (fork) { do_subroutine (); exit; } my $waited_pid = wait; print "The PID of the child upon whom I waited was $waited_pid\n"; print "The value of \$string is ", $string->get(), "\n"; __END__ I am the invoking process and my PID is 23089 The value of $string is First value I am the subroutine and my PID is 23091 The PID of the child upon whom I waited was 23091 The value of $string is Second value

In reply to Re: Perl forked processes and variable sharing by marioroy
in thread Perl forked processes and variable sharing by fireblood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.