12

I have two python applications. I need to send commands and data between them (between two processes). What is the best way to do that?

One program is a daemon who should accept commands and parameters from another GUI application.

How can I make daemon to monitor comands from GUI, while making it's job? I prefer solution would be crossplatform.

p.s. I use pyqt4 and python.

asked Oct 13, 2010 at 9:02
2
  • 1
    Similar to stackoverflow.com/questions/3902997/… Commented Oct 13, 2010 at 9:09
  • Oh, useful link. Thanks. Didn't stumble it while searching. Commented Oct 13, 2010 at 10:51

3 Answers 3

11

You can use the following methods for data interchange:

  1. Socket Programming : In Qt you can access QtNetwork module. See qt assistant for examples

  2. IPC : Use shared Memory implemented in QSharedMemory class.

  3. If this application will run on unix os only, then you can try Posix based message queue etc. for data interchange

  4. DBUS : You will find both python and Qt have DBus based support. In Case of python you need to find the relevant module.

  5. Using Multi Processing module

  6. Using Posix/SystemV based IPC mechanism aka pipes, queue, etc.

answered Oct 13, 2010 at 9:09
Sign up to request clarification or add additional context in comments.

5 Comments

Is it posible to interact between applications using only python? For example with subprocess module and Popen.communicate. You think using Qt library would be easyer?
-1: Failed to mention ordinary pipes and ordinary sys.stdin and sys.stdout.
@S. Lott - I'm not sure sys.stdin would be useful in this case if it's a daemon communicating with a GUI since it's unlikely that once process would be launching the other.
@Dave Webb: Perhaps. However, I've written GUI's which fork subprocesses and communicate with the subprocess stdin and stdout. So, I think it's viable.
@PocketSam You should be able to use multiprocessing module and do it too. I have used multiprocessing module and it works fine.
2

While it's not related to the way of the communication, I recommend checking out the pickle/cPickle module (which can encode objects into string streams and vice versa). Very useful.

answered Oct 13, 2010 at 11:04

Comments

0

Example.

Program_1.py

import pickle
import sys
for i in range(100):
 pickle.dump(i,sys.stdout)

Program_2.py

from __future__ import print_function
import pickle
import sys
while True:
 obj= pickle.load(sys.stdin)
 print( obj )

Usage:

Program_1.py | Program_2.py 

Under Windows, this may exhibit bad behavior because of the way Windows botches up simple file IO redirects.

answered Oct 13, 2010 at 11:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.