Gabriel Huwe
Gabriel Huwe

Reputation: 59

How to make two Python programs communicate?

I have two Python programs, one is a IRC bot, using sockets to connect to an IRC server.

This program has a loop that reads every PRIVMSG from an specific channel.

The second program should get whatever the first program outputs (the PRIVMSG in this case) and runs functions with it.

So, it's basically:

while 1:
    data = irc.recv(2048)
    if data.find("PRIVMSG " + current_channel + " :") != -1:
        send_to_second_program(data)

the second program is

while 1:
    data = get_from_first_program()
    do_stuff(data)

Is there a way to make this happen without using modules? The two programs should be separate.

Upvotes: -1

Views: 5481

Answers (3)

mrvol
mrvol

Reputation: 2973

  1. Over JSON (Rest API). It's service oriented architecture. So, you can very scale your application.
  2. Over Message queue It's scaleable too, but it's not very reliability.

Upvotes: -1

Erwin
Erwin

Reputation: 3366

Although you could use literally dozens of ways to communicate and you provide no context or requirements, I assume that you are working on a small project. I would recommend you to use rpc (remote procedure call). Using rpc you can call Python functions of another application as if it is a function available locally. Check out some rpc library for Python, like http://www.zerorpc.io/, which seems more than enough for your use case.

There are some downsides to using rpc, which you can read about in the answer of this question for example, but if the scope is limited I think this is one of easiest, non-hack, ways to achieve your goal.

Upvotes: 2

aliasm2k
aliasm2k

Reputation: 901

Assuming that both programs are in the same directory, you can import the other file using import

For example, consider a file a.py. To import it in another file b.py use import a Then use a.something to access something defined in file a.py

Upvotes: 0

Related Questions