[Tutor] threading in python2.7
ranceh at gmail.com
Sat Jan 3 05:42:22 CET 2015
On Fri, Jan 2, 2015 at 3:18 PM, Alan Gauld <alan.gauld at btinternet.com>
> 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
>> 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?
Yes, you are correct. Arduino Uno headers on the board, and a full pc on
For $40 (US) its was a no brainer for me. The OS is on an onboard 4GB
Nand, and there is a mini sd card slot and a place to plug in a
conventional sata hd.
Not as many gpio controls on the board as a Pi, but with the easy addition
of some arduino uno hardware that can be resolved.
> 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?
Yes, I did, but it is a dual core cpu.
> So you need a stateless event handler and keep the threading all within
> 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
> 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?
So far all the code is python. The good folks at LinkSprite have python
modules that are board specific so I can get all the gpio controls by
installing the board modules for python and doing a "import gpio"
> 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?
Mine isn't that fancy yet. I have three gpio pins one attached to a red
led, and a second attached to a blue one. The third is controlling a
> <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():
>> # individually starting either lights or sound works as expected
>> # catch keyboard interrupt and call cleanup
>> </pseudo python>
> Where is the event loop?
> You need something to manage the events.
This is where my mistake happened. Sorry I wasn't clear here.
The skeleton code I posted was from the working but not threaded starter
My main looks like this
#thread management stuff here
#start lights thread
#start sound thread
The idea here being that the lights and sounds can pulse on different
frequencies/patterns independent of each other, but both loops running at
what the user feels is "the same time.
> 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.
Got the word loop on the brain. Sorry about this.
The main function and cleanup function are not loops just procedures.
The loops are the light and sound loops and either the main or cleanup
function should be able to set the criteria by which both the light and
sound loops exit.
> 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
>> 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
I'll work up an example that is not board specific and post it.
> Alan G
> Author of the Learn to Program web site
> Follow my photo-blog on Flickr at:
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor