Porting Python Raspberry Pi application to Twisted - how to handle background threads that poll the hardware

733 views Asked by At

I have created a small device that uses a small Raspberry Pi single-board computer to allow me to remotely dispense treats to my cat and monitor him and the surroundings with a web cam. \

The software consist of a single Python module that hosts a web site (to allow remote control of the device), a "thread" to manage the hardware and which runs a simple state machine, and a "thread" that captures pictures via the webcam and which runs a motion detect algorithm to limit captures to times when the cat is around. The web site hosts a single HTML page which contains some Javascript which uses jQuery to make Ajax call-backs to the server.

The software is all working, but I have found that the web site is at times very slow to return the page on initial loading. I think the issue is the general single-threaded nature of Python due to the GIL and the Python simple http server class design.

I am interested in moving the application to Twisted, but I am not sure how to port the two threads that handle the camera and the hardware state machine. Both threads run a loop, effectively polling, with a small delay between each iteration of the loop.

Thanks

1

There are 1 answers

0
Jean-Paul Calderone On

You can use LoopingCall (howto) to schedule a repeated function call on a certain interval. This probably replaces your polling threads entirely.