0

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.

Gabriel Huwe
  • 69
  • 1
  • 5
  • There are a lot of ways to do this. Your particular requirements would have to be used to narrow them down. – jpmc26 Dec 21 '15 at 15:37
  • If they reside on the same machine, use [modules](https://docs.python.org/2/tutorial/modules.html) – SiHa Dec 21 '15 at 15:40
  • @SiHa that's what I'm doing right now. I need it to happen on runtime, though. – Gabriel Huwe Dec 21 '15 at 15:44
  • Please show us some code to demonstrate your problem - see how to write a [mcve]. At the moment it is not possible to answer your problem, because it is very unclear what you actually want to achieve. Also you may be asking the wrong question :) – SiHa Dec 21 '15 at 15:46
  • If they need to be separate processes, consider pipes and signals. If on separate machines (maybe tomorrow) sockets, rpc, REST... – Pynchia Dec 21 '15 at 15:46
  • @SiHa I added some code. – Gabriel Huwe Dec 21 '15 at 15:58
  • Much improved from original post. Good job @GabrielHuwe! – Jeff B Dec 21 '15 at 17:54
  • Why you don't want to use message queue? – mrvol Dec 22 '15 at 18:24
  • @mrvol I looked into them, and Redis looks like exactly what I wanted. Maybe a bit overkill for programs that won't use lots of its features anyway, but thank you. – Gabriel Huwe Dec 22 '15 at 18:55

3 Answers3

2

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.

Community
  • 1
  • 1
Erwin
  • 3,298
  • 2
  • 15
  • 22
0

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

aliasm2k
  • 883
  • 6
  • 12
-1
  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.
samlev
  • 5,852
  • 1
  • 26
  • 38
mrvol
  • 2,575
  • 18
  • 21
  • There are some *very* reliable message queues. Why do you say they're unreliable? – jpmc26 Dec 22 '15 at 16:48
  • First of all, they send and receive message. Second off all, either they may be reliable and persistent or not. It isn't its primary function. I mean, for example, subscriber is down (a few time), but it have skipped a few messages. These messages can broke your logic. For example, you may forget to give money from account. There is a good comparison message queues http://stackoverflow.com/questions/731233/activemq-or-rabbitmq-or-zeromq-or – mrvol Dec 22 '15 at 18:21