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

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


Hi John,

"John Nagle" <nagle at animats.com> wrote in message 
news:4c75768a$0$1608$742ec2ed at news.sonic.net...
>     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)
        DoSomething();
    } // Lock released

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

---Joel




More information about the Python-list mailing list