Can PySerial's write method be called by multiple threads?

Joel Koltner zapwireDASHgroups at
Wed Aug 25 22:24:52 CEST 2010

Hi John,

"John Nagle" <nagle at> wrote in message 
news:4c75768a$0$1608$742ec2ed at
>     You don't need a queue, though; just use your own "write" function
> with a lock.

Hmm... that would certainly work.  I suppose it's even more efficient than a 
queue in that the first thing the queue is going to do is to acquire a lock; 
thanks for the idea!

>    def atomicwrite(fd, data) :
>        with lok :
>            fd.write(data)

Cool, I didn't know that threading.Lock() supported "with!" -- Just the other 
day I was contemplating how one might go about duplicating the pattern in C++ 
where you do something like this:

        Lock lok; // Constructor acquires lock, will be held until destructor 
called (i.e., while lok remains in scope)
    } // Lock released

...clearly "with" does the job here.


More information about the Python-list mailing list