9403kevin
Posts: 3
Joined: Tue Nov 13, 2012 12:40 am

Running a web server in a thread?

Tue Nov 13, 2012 12:49 am

I'm fairly new to Python. I'm building a heating controller, which will be able to be configured via a web interface (and possibly other options such as an Android App later).

The architecture that I had in mind was that the main thread would be the heating control loop, which would be reading sensors, actuating relays and timed operations - the configuration would be held in a database. I was then thinking about running a web server in another thread which would allow the configuration to be changed (updating the database), and upon any changes, it would signal an event to the main thread to re-load it's settings.

However all the web frameworks that i've looked at don't seem to work well operating in a thread, it seems that they are intended to always run in the main thread.

So, is there a suitable simple web framework that will do what i'm suggesting above, or am I on the wrong design track entirely, and there's a much better way of achieving the same thing?

Kevin.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Running a web server in a thread?

Tue Nov 13, 2012 8:16 am

Perhaps I'm misunderstanding something here, but why does it need to be run in a thread under Python? Why not just run it with the OS and talk to it with Python as and when you need to?
Alex Eames RasPi.TV, RasP.iO

9403kevin
Posts: 3
Joined: Tue Nov 13, 2012 12:40 am

Re: Running a web server in a thread?

Tue Nov 13, 2012 10:27 am

alexeames wrote:Perhaps I'm misunderstanding something here, but why does it need to be run in a thread under Python? Why not just run it with the OS and talk to it with Python as and when you need to?
I'm not sure I fully understand what you mean by 'run it with the OS' - the problem is that the 'controller' need to be polling sensors and doing operations on a timed basis too, so isn't purely driven by operations on the web interface.

I could run the web front-end under php (or even a second separate python application), but I think it would be less well connected with the python app. For example it would be harder to notify the python code that there had been a config change (unless the python code was polling the database for changes, or polling a file).

It just seemed like getting everything running under one process seemed like a good idea - but I could be wrong!

Kevin

User avatar
hayesey
Posts: 78
Joined: Mon Nov 28, 2011 12:46 pm
Location: Manchester, England

Re: Running a web server in a thread?

Tue Nov 13, 2012 10:38 am

The python twisted framework might do what you want.

But when I've done things like this in the past I've separated the two parts. Usually good from a security point of view too, bugs in your web interface code have less chance of breaking your controller program. If both applications always sit on the same system then I use a unix socket to pass messages, if they need to be on different systems then listen on a tcp port. Then let apache, lighttpd etc..etc... worry about implementing the actual web server! I have also used XML-RPC to similar effect.

I've actually made a similar system to this already except the difference is the thing doing the controlling of the the heating is an Arduino which communicates with the main controller (which is actually a PC/NAS but could easily be a RasberryPi too, it's just using Python) over a wireless serial link using XRF modules. The Arduino will still operate as a thermostat without the controller being present but the controller provides logging and timer functions. I haven't actually made a web gui yet, I just poke timer entries into an sqlite database, but I will do when I get round to it and will probably do it over tcp or xml-rpc so I can also have an android app talking to it as well.
Last edited by hayesey on Tue Nov 13, 2012 10:44 am, edited 1 time in total.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Running a web server in a thread?

Tue Nov 13, 2012 10:40 am

Perhaps I'm just confused? I just don't see why any of what you want to do requires the python script to 'run' the web server. Your python script can happily run in the background and manipulate/control the server, but if you need precise timings, that will be hard to achieve with linux (if within a second or two is OK, you should be alright :) )
Alex Eames RasPi.TV, RasP.iO

User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

Re: Running a web server in a thread?

Tue Nov 13, 2012 12:12 pm

Run your python program that handles the timed stuff as a daemon or service, in the background.

Even though you do not need a web framework for that part, import the web.py module for database, web.db, so you can have a simple abstraction layer for the database that you can reuse for the other piece of the puzzle, the web application. web.db supports sqllite which seems perfect for this task (supports oracle, mysql and postgresql too). It provides query(), select(), insert(), update() and delete() methods (along with a bunch of others).

For the web app, you use web.py and you can either run it directly (in which case it uses CherryPy as the web server) or you can run it as cgi, fastcgi or wsgi under a variety of web servers. You use the db module again to write the config changes to the database.

I typically break down a web.py app into this kind of structure at a minimum (I use 5 more directories for documentation, tests, mapping for all database objects etc):

Code: Select all

index.py
static/
templates/
You put your html templates in templates/, javascript and images in static, and index.py is your web application. index.py will dispatch or route all URLs.

If you need to communicate between the two process, beside using the database, to issue a shutdown, restart etc for example, or to display the data in real time, then I would use zeromq. And for Python the binding is pyzmq.

If you need real time refresh on the client side, use jquery to issue a get request to a url on your web app that will spit out JSON data back to the browser and display it.

François
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

Return to “Python”