Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple inter-process communication

Tags:

python

perl

ipc

I'm looking for a simple way to pass messages from one process (Perl script, short-lived) to another (Python script, long-running) - both processes local to the same machine. I've done some research, but what I've found was either over my head or seemed unnecessarily complex - leaving me a bit lost and confused.

I imagine a minimal example roughly like the following:

# listener.py

class Listener:
    def __init__(self, port)
        self.port = port

    def on_message(self, msg):
        print "%s: %s" % (timestamp, msg)

recipient = Listener(1234)


# sender.pl

sub send_message {
    my ($msg, $port) = @_;
    # ...
}

send_message("hello world", 1234);

Any pointers on how to solve and/or where to read up on this would be greatly appreciated!

like image 354
AnC Avatar asked Feb 09 '11 20:02

AnC


1 Answers

It turns out that interprocess communication is, while on the surface straightforward, actually fraught with complications. Whatever anyone tells you here in terms of a simplified answer, always keep in mind that there is probably a lot of caveats that are being left unsaid.

Now with that disclaimer out of the way, I claim that what you likely want are message queues. This is based on the fact that you did not include an ip address in your example api. If you need to go across machines, you will want sockets. However, I think you will find message queues to be simpler to understand if you can deal with the fact that this is only for communicating with processes on the same machine.

A good starting point for perl is:
http://perldoc.perl.org/IPC/Msg.html

for python, this seems to explain (ignore the other kinds of ipc like semaphores):
http://semanchuk.com/philip/sysv_ipc/

like image 199
frankc Avatar answered Sep 29 '22 05:09

frankc