Search code examples
pythonasynchronoustwisted

Simultaneously log and take commandline input using Twisted


I am building an app using Twisted in which a server contains some timers and several times a second it writes the timer values to a client over UDP. The client needs to be able to send various commands (such as "pause timer 1" or "reset timer 2") to the server to change the timers being sent to the client. The problem I see is that the client is constantly logging the timer values received from the server so I'm not sure how I can do this without blocking. I want to do this asynchronously, without any threading. Twisted provides numerous asynchronous IO options but I'm really not sure which would be the most appropriate because some of them (such as the basic.LineReceiver protocol) are still blocking while waiting for the user input.

Thanks in advance, Ben

P.S. this is my first post so please let me know of any improvements I could make.


Solution

  • You can use twisted.internet.stdio for non-blocking standard I/O. This provides an alternate streaming, ordered, connection-oriented transport that can be used with any protocol that normally runs on such a transport (other similar transports include TCP and SSL).

    See stdin.py and stdiodemo.py linked from http://twistedmatrix.com/documents/current/core/examples/

    Also, LineReceiver isn't "blocking while waiting for the user input". It's just a protocol, it doesn't even do any I/O itself.