On Thu, May 26, 2011 at 9:38 AM, <exarkun@twistedmatrix.com> wrote:

In addition to what Andrew said, I'll also point out that rather than
making code that expects blocking writes to stdout work by putting
stdout back into blocking mode, you can make it work by instead
providing a stdout which will buffer anything that cannot immediately be
written non-blockingly.  StandardIO gives you exactly this; its `write`
knows how to deal with non-blocking descriptors and buffer as necessary.
This is why manhole doesn't have the same problem as your code calling
directly into `code.interact`.  For details,
<http://twistedmatrix.com/trac/browser/trunk/twisted/conch/manhole.py#L45>.


Thanks.  I'm having trouble integrating this or Andrew's idea into my example.  The simplest case doesn't quite work right for me (I see '>>>' prompts, and can interact with the python interpreter but the banner appears at the end of the REPL after I press Ctrl+D, and then I never see the '>' prompts from my original program anymore)...was this what you had in mind?  See the diff below.

@@ -6,6 +6,7 @@
without blocking the reactor.
"""
 
+import sys
from twisted.internet import stdio
from twisted.protocols import basic
 
@@ -22,7 +23,8 @@
         code.interact()
 
def main():
-    stdio.StandardIO(Echo())
+    stdio_obj = stdio.StandardIO(Echo())
+    sys.stderr = stdio_obj
     from twisted.internet import reactor
     reactor.run()


--
Benjamin