[Twisted-Python] simple spawnProcess
exarkun at divmod.com
Wed Aug 10 02:04:14 EDT 2005
On Tue, 9 Aug 2005 16:34:17 -0600, jmbenski at micron.com wrote:
>I'm new to twisted, Python, and network programming in general, so this question has probably been answered many times. Although after searching for a few days I haven't seen it.
>I'm trying to set up a simple process that accepts data through stdin and prints the response to stdout. It is a continuously running process that will keep waiting for more input on stdin and will continue writing to stdout until a shutdown command is sent to it. I started the reactor in a separate thread so that my main thread doesn't block while waiting for a response and I can send commands to the spawned process.
Why are you using threads at all? Twisted's process support presents an asynchronous API, like most other things Twisted does. You don't need threads here. Their presence is what is breaking your program. You cannot call reactor.run() from one thread and /any/ other Twisted API (unless explicitly marked as an exception) from a different thread. In particular, reactor.spawnProcess() and transport.write() in the code you included are being called from the wrong thread.
More information about the Twisted-Python