Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Modifying an object in a fork

by 1nickt (Monsignor)
on Mar 11, 2018 at 23:45 UTC ( #1210694=note: print w/replies, xml ) Need Help??


in reply to Modifying an object in a fork

It's trivial using the mighty MCE::Shared by marioroy, two extra lines:

package MyClass { use Moose; has foo => ( is => 'rw', default => 'bar' ); }; package main { use strict; use warnings; use feature 'say'; use MCE::Shared; my $obj = MCE::Shared->share({ module => 'MyClass' }); say 'Before forking: ' . $obj->foo; my $pid = fork; if ( $pid == 0 ) { $obj->foo('baz'); exit; } else { waitpid $pid, 0; } say 'After forking: ' . $obj->foo; };
Output:
$ perl 1210687.pl Before forking: bar After forking: baz
For details see the documentation on sharing objects between processes with MCE::Shared.

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Modifying an object in a fork
by marioroy (Priest) on Apr 29, 2018 at 11:03 UTC

    Hi 1nickt,

    It's trivial using the mighty MCE::Shared ...

    Also, see the logger and Tie::File MCE::Shared demonstrations. The object class, like in your demo, is constructed under the shared-manager process where it lives. How it works is that any interaction with the object is then possible via the OO interface only.

    Regards, Mario

Re^2: Modifying an object in a fork
by nysus (Priest) on Mar 14, 2018 at 18:05 UTC

    Thanks, hadn't heard of that module before.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210694]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-07-23 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (462 votes). Check out past polls.

    Notices?