Schuitz
Posts: 15
Joined: Sun Jan 27, 2013 1:17 pm

General inter-process communication in python

Wed Feb 13, 2013 10:15 pm

I have seen a few threads with folks asking how to make things happen (eg. twiddling GPIOs) via a web server interface, but what I would like to do is to have some basic interaction through the web with a python process that is already running on the pi. The context is a pretty simple standalone picture frame installation that is pulling some 10,000 images from my NAS, displaying them (one at a time!), and providing some basic user features like navigation with a LIRC remote, exclude/favourite lists, and other bells and whistles TBD.

To go to the next level, I would like to set up a lightweight web server, access the page on my tablet/phone etc, and have some basic control features available there, extending it to show EXIF data of the currently displayed image, for example, and perhaps going to town and navigating with swiped thumbnails. :roll:

I have some rudimentary C/C++ experience but am new to the world of sockets, threads, signals etc. So I am looking for some very general arm-waving guidance pointing to relevant topics. My best guess right now is for the running process to save it's pid into /tmp, say, and have this picked up by the web script. From there I could send signals from the web script using os.kill, if permissions can be set up to allow that.

But this seems a little gross, and I'm pretty certain that's not even close to correct given the small subset of signals provided by the os. Where to begin? I would be thrilled at this point just to present a web page with two buttons, 'next' and 'prev', and send those two commands to the running process, but I would like to do it the 'right' way, a way that can hopefully be extended later.

User avatar
aTao
Posts: 1095
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: General inter-process communication in python

Wed Feb 13, 2013 11:22 pm

Why not use a postbox system. The web server sets instructions in a file that the python process reads every now and then (just before every picture rerfesh for example).
>)))'><'(((<

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: General inter-process communication in python

Thu Feb 14, 2013 11:37 am

So your picture frame 'slideshow' application is written in Python?

One option would be implementing a simple HTTP server within that application:
- API doc http://docs.python.org/2/library/basehttpserver.html
- Raspberry Pi Educational Manual http://www.raspberrypi.org/archives/2965 page 120 (pg 101 in the PDF, at least on my reader)

Obviously on Linux there are numerous other options:
- sockets and TCP/IP
- named pipes
- FIFO files
- XMLRPC (ed man pg 118/99)
- ...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Schuitz
Posts: 15
Joined: Sun Jan 27, 2013 1:17 pm

Re: General inter-process communication in python

Thu Feb 14, 2013 3:46 pm

Thanks for the responses so far, these are very helpful suggestions.

Correct, the slideshow is written in python/pygame. Since I've discovered the consequences of no OpenGL ES integration there I plan to do a rewrite for pi3d, or maybe stephanh's recently announced rpigl code. I'm doing some basic image transformations based on the aspect ratio, some alpha transitions between images, and a graphic menu UI for the IR remote part.

I didn't even know a basic http server was available in python. I will dig into this. I was also able to run some of the basic socket tutorials on Doug Hellmann's site, and will learn about some of the other options in your list.

richardp
Posts: 117
Joined: Thu Jan 12, 2012 11:46 am

Re: General inter-process communication in python

Thu Feb 14, 2013 5:03 pm

D-BUS .. thats the flavour of the day for my IPC processes.

Its already there. There is no right/wrong way to achieve your task, its what you feel comfortable doing.

R
RaspberryPi's galore
Solid run CuBox
ODroid U2

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: General inter-process communication in python

Thu Feb 14, 2013 7:28 pm

One more - good thing I left the ellipsis there ;)

http://dbus.freedesktop.org/doc/dbus-faq.html
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: General inter-process communication in python

Fri Feb 15, 2013 11:28 am

I've written a simple gpio-daemon which runs in the background and can receive commands from any other process via unix sockets. Take a look - perhaps it could relate to what you want to do: https://github.com/metachris/raspberrypi-utils
pythonhosted.org/RPIO

arista
Posts: 4
Joined: Sat Aug 11, 2012 10:13 am

Re: General inter-process communication in python

Sun Feb 17, 2013 6:56 pm

metachris wrote:I've written a simple gpio-daemon which runs in the background and can receive commands from any other process via unix sockets.
I was looking exactly something like this, looks promising. I have a tornado based web front end, which should send commands to the the backend, which then controls digital RGB LED strip. I'm currently transforming gpiomanager.py to stripmanager.py...

Would it be a good idea to make this more general and extensible daemon?
--
Aapo Rista

Return to “Python”