gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monks,
I am writing some code where I have two processes created using fork() which communicate via sockets. I need to have only one of those processes execute functions, but the call to those functions may come from either process. So, I am trying to come up with a scheme to pass a function call, along with all arguments, from one process to the other via the socket. Here's what I have so far:
I have tried using the FreezeThaw module to serialize/deserialize the arguments on opposite sides of the socket. However, this does not work for arguments that are references, such as the 3rd argument in my example. Any ideas how else I can make such a remote function calluse warnings; use strict; use Socket; use IO::Handle; use Data::Dumper; socketpair(WORKER_SOCKET, GUI_SOCKET, AF_UNIX, SOCK_STREAM, PF_UNSPEC) + || die "socketpair: $!"; WORKER_SOCKET->autoflush(1); GUI_SOCKET->autoflush(1); if (my $pid = fork()) { close GUI_SOCKET; my $line = <WORKER_SOCKET>; if ($line =~ /^EXECUTE:(.*)$/) { # my $fx = "extracted function name"; # my @args = "extracted args" &{\&{$fx}}(@args); } } else { die "cannot fork: $!" unless defined $pid; close WORKER_SOCKET; # redirect the STDOUT of the WORKER script to GUI (and main Bacnet +Tool # communication) process. STDOUT->autoflush(1); my $a = 'baz'; my @b = qw(foo bar); my $c = \@b; print GUI_SOCKET "EXECUTE:doit($a, @b, $c)\n"; } sub doit { my @q = @_; print "\nDoing it " . Dumper(\@q); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Executing functions from another process
by davido (Cardinal) on Jan 23, 2014 at 19:11 UTC | |
by gri6507 (Deacon) on Jan 23, 2014 at 19:25 UTC | |
by Corion (Patriarch) on Jan 23, 2014 at 19:29 UTC | |
by gri6507 (Deacon) on Jan 23, 2014 at 19:54 UTC | |
by Corion (Patriarch) on Jan 24, 2014 at 07:29 UTC | |
by Anonymous Monk on Jan 23, 2014 at 23:09 UTC | |
| |
Re: Executing functions from another process
by jethro (Monsignor) on Jan 23, 2014 at 20:13 UTC |
Back to
Seekers of Perl Wisdom