[Tutor] threading in python2.7
Alan Gauld
alan.gauld at btinternet.com
Fri Jan 2 22:18:49 CET 2015
On 02/01/15 20:17, Rance Hall wrote:
> I bought myself a pcduino 3 nano development board for Christmas and
> started picking up python again after a long forced hiatus. The board runs
> Ubuntu Precise
Snap!(ish), I got an arduino Uno and RaspberryPi.
As I understand it the pcDuino is just a PC and
duino on a single board, right?
> Working with development boards has me navigating the inner working of
> python threading and I find myself at a loss.
You realise the 'duino processor is strictly single threaded?
So you need a stateless event handler and keep the threading all within
Python.
> My current project is a simulated emergency vehicle warning system
> (otherwise known as a police siren)
>
> I started my project by writing the lighting and the sound portions of the
> program separately and tweaking them till I got each part working as
> desired.
How are you writing the code?
Are you writing 'duino stuff in C and control stuff in Python?
How does the pcduino communicate between the controller and PC sections?
Do you still use the 'duino IDE?
In my set up I'd write the light/sound controller stuff and have
the event loop read signals from the Python code via the USB link.
Is it similar on the pcduino?
> <pseudo python>
> def setup_hardware():
> do stuff here to setup hardware and gpio sytem
>
> def lights():
> control the red and blue LEDs to simulate warning lights
>
> def sound():
> control the sound to simulate a warning siren
>
> def cleanup():
> exit cleanly and cleanup
>
> def main():
> setup()
> try:
> # individually starting either lights or sound works as expected
> except:
> # catch keyboard interrupt and call cleanup
>
> </pseudo python>
Where is the event loop?
You need something to manage the events.
> Each of the lights and sound functions are placed in a "while not
> exitFlag:" loop
So the thread control is in two separate loops.
And each loop can set the flag?
> exitFlag variable made in the either the main loop or the cleanup loop are
> never seen by the lights and sound loop threads.
How many loops do you have? You have a light loop, a sound loop, a setup
lop and a cleanup loop? Sounds too many.
> The net effect of this is that once the threads are started, they are
> uncontrollable and I have to quit the terminal to get the threads to stop.
>
> Could someone please point me in the direction of a good tutorial or sample
> code that works that I could use as study tool?
You should probably try the Ardiuino/PCduino forums
> I've not bothered posting the code since its custom to this board and will
> fail on another python instance
But your skeleton is too skeletal for us to suggest anything. You don't
even show us where the threading happens, or how. You could replicate
the code structure using a pseudo API that just prints or returns values
from a list...
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list