I know there are other questions quite similar to mine, but none of them address the issue I'm having.
I'd like to use pyserial to access a serial port (/dev/tty...), but only on the condition that another process hasn't already opened it.
The following snippet returns four available ports on my Ubuntu 12.04 machine, when run once. If I run it a second time I would expect no ports to be available. Sadly, the same list of ports are returned. It seems pyserial cannot identify that another process has already opened the port.
I'd expect a SerialException to be thrown, or the isOpen() method to return False, but pyserial happily opens the multiple times.

import serialfrom serial import toolsfrom serial.tools import list_portsdef available_ttys(): for tty in serial.tools.list_ports.comports(): try: port = serial.Serial(port=tty[0]) if port.isOpen(): yield port except serial.SerialException as ex: print 'Port {0} is unavailable: {1}'.format(tty, ex)def main(): ttys = [] for tty in available_ttys(): ttys.append(tty) print tty input('waiting ...')if __name__ == '__main__': main()This is the output regardless of how many times I run it in parallel:

Port ('/dev/ttyS31', 'ttyS31', 'n/a') is unavailable: Could not configure port: (5, 'Input/output error')...Port ('/dev/ttyS0', 'ttyS0', 'n/a') is unavailable: Could not configure port: (5, 'Input/output error')Serial(port='/dev/ttyUSB1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)Serial(port='/dev/ttyACM2', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)Serial(port='/dev/ttyACM1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)Serial(port='/dev/ttyACM0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)waiting ...

Check Solution