Can PySerial's write method be called by multiple threads?
nagle at animats.com
Wed Aug 25 22:01:01 CEST 2010
On 8/25/2010 11:36 AM, Joel Koltner wrote:
> I have a multi-threaded application where several of the threads need to
> write to a serial port that's being handled by pySerial. If pySerial
> thread-safe in the sense that pySerial.write behaves atomically? I.e.,
> if thread 1 executes, serport.write("Hello, world!") and thread 2
> executes serport.write("All your bases are belong to us!"), is it
> guaranteed that the output over the serial port won't "mix" the two
> together (e.g., "Hello All your bases are belong to us!, world!") ?
You're not guaranteed that one Python "write" maps to one OS-level
"write". Individual "print" statements in Python are not atomic.
You don't need a queue, though; just use your own "write" function
with a lock.
lok = threading.Lock()
def atomicwrite(fd, data) :
with lok :
More information about the Python-list