I'm trying to write a serial 'proxy' that will sit between a process and a physical serial device and re-write some commands on the fly. I had it working in a crude manner but it was much slower than going straight to the physical device.
In an effort to speed it up, I've stripped out everything other than the bare read/write, but performance is much slower than expected.
Any ideas what I'm doing wrong? I am very new to this low level stuff, most of my limited experience is with parsing json / xml / etc and manipulating strings. Any thoughts or hints would be much appreciated.
Heres the simplified code-
def host_thread( host_port, dv_port ):
    while True:
        try:
            char = os.read(host_port, 1)
            os.write(dv_port, char)
        except OSError as err:
            if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
                pass
            else:
                raise  # something else has happened -- better reraise
def dv_thread( host_port, dv_port ):
    while True:
        try:
            char = os.read(dv_port, 1)
            os.write(host_port, char)
        except OSError as err:
            if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
                pass
            else:
                raise  # something else has happened -- better reraise
if __name__ == "__main__":
    host_port, slave = pty.openpty()
    print("Connect to port: %s" % os.ttyname(slave))
    fl = fcntl.fcntl(host_port, fcntl.F_GETFL)
    fcntl.fcntl(host_port, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    dv_port = os.open("/dev/ttyUSB0", os.O_RDWR | os.O_NONBLOCK)
    host_thread = threading.Thread(target=host_thread, args=[host_port, dv_port])
    host_thread.start()
    dv_thread = threading.Thread(target=dv_thread, args=[host_port, dv_port])
    dv_thread.start()
    while True:
        time.sleep(.1)
