#33 support for python's builtin asyncore


with some rather simple additions, serial could be used together with asyncore (a python builtin method for asynchronously working with socket style connections) and asynchat (which builds on top of it).

asyncore expects a socket-like interfaces; when just trying to plug them together, i found that four functions are required: setblocking, getpeername, recv and send.

all of them can be implemented trivially by aliasing or short lambdas:

serial.Serial.setblocking = lambda self, blocking: self.setTimeout(None if blocking else 0)
serial.Serial.getpeername = lambda self: self.port
serial.Serial.recv = serial.Serial.read
serial.Serial.send = serial.Serial.write

the getpeername seems to be relevant with network sockets where the peers can vanish; some other functions around the corners might still be needed (for disappearing devices and such), but for what i've needed so far, these four lines are all that's needed to integrate with python's own asynchronity framework.


  • Niko Will

    Niko Will - 2012-09-28

    That would be a great enhancement!

  • Chris Liechti

    Chris Liechti - 2013-10-11
    • status: open --> pending
    • assigned_to: Chris Liechti
    • Group: --> Next Release (example)
  • Chris Liechti

    Chris Liechti - 2013-10-11

    I guess this will only work under posix systems, as it uses select and under Windows, for example, select only accepts sockets.

    As it seems to be relatively simple to achieve that and that I do not think that there would be many users, I'd prefer to leave that to the user. Doing the code above or making a small mix-in class seems to be trivial for those who really need this. (It could be considered to add such a mix-in class to serial.tools)


Log in to post a comment.