3

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.

1 Answer 1

1

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.

2
  • Thanks a lot, Jean-Paul. I had taken a look at twisted.internet.stdio earlier but it didn't seem like what I was looking for. Looks like I still have some learning to do with regards to Twisted!
    – Ben S
    Nov 9, 2011 at 18:33
  • Looks like the examples given don't work on Windows, for some reason the Win32Api keeps setting the value of stdin to 3 and stdout to 7 (instead of the twisted-expected 0 and 1). I'll instead implement a different method using raw_input and threads.deferToThread(). And maybe start developing on Linux...
    – Ben S
    Nov 10, 2011 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.