开发者

How to interchange data between two python applications?

开发者 https://www.devze.com 2023-01-19 23:41 出处:网络
I have two python applications. I need to send commands and data between them (between two processes).

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 parame开发者_如何学Pythonters 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消