[Twisted-Python] Advice on porting Python application to Twisted

Matthew Humphrey mhumphrey at gmail.com
Mon Aug 26 21:06:03 MDT 2013


I recently created a small automated, remote (via web) controlled pet
feeder using a Raspberry Pi single-board computer. The software is all in
Python, and uses the simple HTTP server that is part of the Python
libraries. I discovered Twisted about 2/3 of the way through the project,
and now that I am done I would like to port it to use Twisted. After
reading much of the documentation the Twisted web site, some aspects of
this are fairly obvious to me, while others are not. I could use some
advice on how best to design my app to fit in with the Twisted framework.

Here are the major components of the application:

1) A web site that hosts a combination of static content and some REST
APIs. REST apis are called from Javascript Ajax, and do things like
enable/disable the webcam, dispense a treat, etc. The static content
consists of a single HTML page, some javascript scripts, and a JPG that is
captured by #3 below.

2) A thread that runs a loop which manages the hardware. It does this with
a simple state machine composed of a base class and subclasses for all the
states that the hardware can be in (starting, idle, displaying status on
the lcd, dispensing a treat, recovering from treat dispense cycle, shutting
down). After initializing the hardware and the initial state, the thread
loops continuously calling a "timeTick" event to the current state. WIthin
the states, the code looks at various hardware status (like whether a
button is pressed) and decides to take action or to trigger transition to
another state.

3) A thread that runs continuously capturing images from a webcam. This
thread captures low resolution images continuously, and compares sucessive
frames to see if there is significant number of different pixels (motion
detect). If so, it captures a higher resolution image, updates a symbolic
link to point to the most recent image captured, and deletes any excessive
files from previous captures. The images captured for the motion detect are
handled by executing a process and capturing the stdout. The higher res
images captured by executing a process that writes directly to a file.

Porting #1 is very straightforward after going through the tutorial on
Twisted web. However, I am not sure how to handle #2 and #3. I would
appreciate some suggestions from those who are familiar with Twisted.

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20130826/9da2a403/attachment.html>


More information about the Twisted-Python mailing list