[Twisted-Python] simple spawnProcess

glyph at divmod.com glyph at divmod.com
Wed Aug 10 16:37:42 EDT 2005

On Wed, 10 Aug 2005 08:53:48 -0600, jmbenski at micron.com wrote:
>Ok, obviously I'm missing something in the API.
>When I call reactor.run(), my main thread is stuck in the loop.  I tried to set up my code in a thread to get around this.

That is rather the point.  Using threads to "get around this" will break Twisted.  Threads are not supported for most APIs, and as JP said, they are what is breaking your program.

>    ##At some unknown point in the future I might need to issue commands

You are correct that your program will never get there.  Code after run() is not a place that your program should really ever *expect* to get.

If you need to run some code 'in the future', see reactor.callLater.  In general though, you will have to issue commands to your spawned process in response to some event.  Was it a button click?  Use Twisted's integration with a toolkit (such as GTK or Qt).  Was it a network request?  Set up a ProtocolFactory with a reference to your spawned process, or set up one that spawns processes itself when network connections are made.

There are lots of events which can potentially hpppen while the main loop is running.  Think of your program as a series of responses to those things, and the bit before reactor.run() as simply the set-up to make sure appropriate events get caught (your initial callLater to set up a timed loop, or a listenTCP to connect your program to the network) rather than as the program in its entirety.

More information about the Twisted-Python mailing list