RE: [Twisted-Python] simple spawnProcess

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. from twisted.internet import reactor import os import sys import time class procProtocol(protocol.ProcessProtocol): def __init__(self): pass def connectionMade(self): print "Connection Made\n" def outReceived(self, data): file = open("bob.txt","a") file.write("outReceived\n" + data) file.close() executable = "foo" program = "MSsync" args = ["arg1","arg2", "path", str(os.getpid())] #create the handler for the communication processProtocol = procProtocol() reactor.spawnProcess(processProtocol, executable, args, env=os.environ, path=args[2], uid=None, gid=None, usePTY=True) #launch the reactor to grab events. reactor.run() ### I will never get here ### ##At some unknown point in the future I might need to issue commands processProtocol.transport.write( "some command \r\n" ) processProtocol.transport.write( "some command \r\n" ) -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Jp Calderone Sent: Wednesday, August 10, 2005 12:04 AM To: Twisted general discussion Subject: Re: [Twisted-Python] simple spawnProcess On Tue, 9 Aug 2005 16:34:17 -0600, jmbenski@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. Jp _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Wed, 10 Aug 2005 08:53:48 -0600, jmbenski@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.

On Wed, 10 Aug 2005 08:53:48 -0600, jmbenski@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.
participants (2)
-
glyph@divmod.com
-
jmbenski@micron.com