Async serial communication/threads sharing data

Scott David Daniels Scott.Daniels at Acm.Org
Sat Mar 21 17:51:50 EDT 2009


Nick Timkovich wrote:
> I've been working on a program that will talk to an embedded device
> over the serial port, using some basic binary communications with
> messages 4-10 bytes long or so...  Ultimately, this program ...
> [send] messages and collecting responses for 10k-100k cycles ...

Here's a simple structure:
One thread talks to the device, it passes commands to the device,
collects the response, and (when a full response is collected),
sends the response back.  Queues have all the locking needed, so
most of us just use Queues to communicate between threads (as
Paul Rubin has suggested).  For example:

    def thread_body(command_queue, result_queue):
        for command, reps in iter(command_queue.get, None):
            result = []
            try:
                for n in range(reps):
                    device.send(command)
                    result.append(device.read(reply_length(command))
            finally:
                result_queue.put(result)

Then you just do something like:
     import queue
     commands = queue.Queue()
     replies = queue.Queue()
     start_some_thread(thread_body, commands, replies)
     ...
     some loop:
         commands.put(fetch_voltage, 50)
         print(replies.get())
         ...
     commands.put(None)

Here the device-talker works away while it can, waiting
until it has a command, and waiting for all replies or
some failure to get back to the caller.
The caller may either block for thread replies, or use
replies.get(timeout=7.25) or replies.get(block=False)
to wait (or not) and get a queue.Empty exception to signal
"no data yet".

 > Does the threading.Lock object just prevent every other thread from
 > running, or is it bound somehow to a specific object (like my list)?

A Lock instance can be used to protect as many resources as you like;
it really only decides who gets the lock; you decide what you only do
while holding the lock (the other threads are running, except those
waiting for the lock).

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list