[Twisted-Python] how to ensure writes to stdout via stdio.StandardIO are completed before we shutdown the reactor?

Benjamin Rutt rutt.4 at osu.edu
Tue Jan 31 22:59:30 EST 2012


Hi twisted community,

What’s the best way to ensure writes to stdout via stdio.StandardIO
are completed before we shutdown the reactor?   The use case is to
emit an error message before we shutdown the reactor and stop our
program.  Dummy code snippet:

--------------------------------------------
#!/usr/bin/python

from twisted.internet import reactor, stdio
from twisted.protocols.basic  import LineReceiver

class StdinInteractor(LineReceiver):
  delimiter = '\n'

def go():
  stdio_proto = StdinInteractor()
  stdio.StandardIO(stdio_proto)
  stdio_proto.transport.write('did we get this message?\n')
  if 0:
    reactor.stop()
  else:
    reactor.callLater(1.0, reactor.stop)

reactor.callWhenRunning(go)
reactor.run()
--------------------------------------------

If I change ‘if 0’ to ‘if 1’, I don’t get the message on stdout.  The
‘reactor.callLater(1.0, reactor.stop)’ is obviously a hack to let the
reactor send out the buffered message before shutting down.  Any
ideas?

Thanks,

-- 
Benjamin Rutt



More information about the Twisted-Python mailing list