Imagine inter-processes communication
+--------------+ +------------+
| main_process | ==produces data somewhat to=> | monitoring |
+--------------+ +------------+
where:
- the
main_processis running non-stop and produces some data formonitoring - the
monitoringis running only sometimes, and when it is running it should read the data produced bymain_process - and when the
monitoringis not running the data produced bymain_processshould be not saved.
The question is: How to write like “on demand” IPC?
The code for main_process is basically the next (the real one is more complicated):
use 5.014;
use warnings;
my $box = new BlackBox( callback => sub {
my ($self, $jref) = @_;
#
# processing of $jref
#
});
$box->run();
The callback is called every 2-5 seconds and as i told above, this process should run non-stop. I can’t change the BlackBox.
I need:
- write the
processing of $jrefpart – what should send$jrefto somewhere - and the
monitoringprocess itself, what should read the data, when it is running…
Don’t need any code, need only some pointers to the right direction, or idea how to do this, without filling up my memory or HDD, so the simplest way:
- write the $jref to the file is not suitable because it will fill my HDD when the
monitoringis not running.
If someone care, the $jref is a reference to json string, so i can do:
use JSON::XS qw(decode_json):
my $perlref = decode_json($$jref);
My first thought was “UDP to localhost?”
Variations on that idea include AF_UNIX or a named pipe. With a stream socket you’d do a non-blocking connect, and with the pipe you’d do O_WRONLY|O_NONBLOCK, and if you get EAGAIN just return without writing.
You can save your file handle and reuse it across multiple calls, just close it and reopen if you get
EPIPE. You’ll want$SIG{PIPE}='IGNORE';hopefully the black box doesn’t object to that.The reading side is as simple as
cat $path_to_fifoornc -l -u -p $udpport, slightly harder if you do an AF_UNIX socket.