ZiBaT => Peter Levinsky => IoT=> exercise

Threading and Queueing

Updated : 2017-03-01


Threading and Queueing

Idea: To understand how to program threading and queueing in Python

Reason: Later you are going to implement sensor handling in
seperate threads which all send the messages in a queue to
a thread which take action (eg. using a state machine) of the incomming sensor values.

Background: Example: Threading https://pymotw.com/3/threading/ and Queue https://pymotw.com/3/queue/
                       Previous exercise statemachine

Reference: Threading: https://docs.python.org/3/library/threading.html and Queue: https://docs.python.org/3/library/asyncio-queue.html

 

The idea

To make a good design for an application to read and control devices from a Raspberry Pi, you can follow this :

Sensor program design

In the following you will move forward this solution step by step.

 

Step 1 : Create threads

Create a python project, where you create 15 thread with workers, having different names, to print out their names.

Extra : Inherit from the thread class and override the run method and make an instance of the subclasses to be stated.

Step 2 : Producer Consumer in Python

Create two different thread on as a producer and one as the consumer.

The Producer make / produce items - it can be simple strings and put then in a queue. (hint in the start make a for loop and produce XX items).

The Consumer takes the items from the queue and prints them.

Extra: use a priority queue in stead of the the straight normal queue.

 

Step 3 : Combine

Now let one thread get input from the joystick and another carry out the action eg. let the light show red for left and blu for right etc.

 

Step 4: Real Sensors

Make a seperate thread to read input from the PS2 game-button, and send the information in a queue.
Make a thread to read from the queue and use a statemachine to determinate the next state and move.
Make a thread to write out the next move to some device (simple console, 8*8 Led, GUI ...)