19

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!

AnC
  • 4,099
  • 8
  • 43
  • 69
  • 2
    Why aren't you using the OS pipelines available directly in the shell? `python somescript.py | perl otherscript.pl` should work nicely. No sockets. – S.Lott Feb 09 '11 at 20:35
  • Via a POSIX-MQ style message send or can you solve with sockets? – Jé Queue Feb 09 '11 at 20:50
  • 1
    The Python script is a long-running process, and piping messages only works on a one-off basis (I think - you can't pipe into existing processes, can you?). To make up a silly example, imagine `Listener` was a desktop widget displaying incoming messages (kinda like `tail -f mylog`). – AnC Feb 09 '11 at 20:50
  • @AnC: Then **update** your question to include those facts, please. – S.Lott Feb 09 '11 at 21:13
  • How about unix socket / local socket that are acceded as files? – Aif Feb 09 '11 at 21:40
  • 1
    @S.Lott: I thought I did!? Anyway, I'm currently reading up on sockets and message queues, will accept an answer when I have more of an insight. – AnC Feb 10 '11 at 07:32
  • @AnC: "I thought I did!?" You can think that. If someone asks, though, consider this possibility: it's not clear enough. Further your description of this mythological python process which is "long running" is too vague to guess at what it does or how you can interact with it. Why isn't this magical process a proper server? Why doesn't it implement HTTP/REST and WSGI? – S.Lott Feb 10 '11 at 11:59
  • @S.Lott: Your comments seem quite abrasive. While AnC's post is a bit muddled it was clear enough to me to get an idea of what he wanted. And the implication that any system receiving a message should be done using HTTP is a bit odd. What if he is writing a program to send commands to a CNC lathe (random example) that needs to listen for abort requests? An embedded HTTP server may be over kill. – Ven'Tatsu Feb 10 '11 at 19:11
  • @Ven'Tatsu: "What if he is writing a program to send commands to a CNC lathe" That's my point. We don't know much, do we? We can guess, but without facts, HTTP is either perfect, not enough or overkill. – S.Lott Feb 10 '11 at 19:19
  • 1
    FWIW, I wasn't looking for the optimal solution to a specific issue, but trying to learn about the concepts in general - that's why I kept the OP fairly abstract/generic. The answers have provided me with enough data to get a basic idea of what's what, so I'm content with the outcome, and I'm confident it will prove useful to others in a similar situation. – AnC Feb 11 '11 at 09:54

3 Answers3

20

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/

frankc
  • 11,290
  • 4
  • 32
  • 49
  • Quoting from the Python link: _This describes the sysv_ipc module which gives Python access to System V inter-process semaphores, shared memory and message queues on most (all?) ***nix flavors**. Examples include OS X, Linux, FreeBSD, OpenSolaris 2008.11, and (maybe) AIX. It might also work under Windows with a library like Cygwin._ – ChaimG Feb 07 '17 at 18:35
11

And for powered-up communications in the same style as socket, consider looking at 0MQ. It can make use of different communication technologies depending where your two apps are located, and even for local processes it's very easy to use and solves the problems for you.

http://zeromq.org

chickenkiller
  • 426
  • 2
  • 5
3

In general you are interested in sockets. A good place to get just the needed rough information is the documentation of IO::Socket::INET or more basic socket-stuff in perl from perldoc perlipc

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Sven Eppler
  • 1,646
  • 1
  • 15
  • 26
  • For the moment, I'm going with sockets - mostly because I found a concise tutorial: http://www.doughellmann.com/PyMOTW/socket/tcp.html I'll keep reading up on this topic though, as it seems there's lots to be learned. – AnC Feb 10 '11 at 19:38