[Twisted-Python] simple spawnProcess
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. If I run this program without the sleep everything works correctly. If I add the sleeps before the write, the write never registers with the process. It is almost as if the pipe never gets flushed with the transport write. I've looked through the API and haven't seen a way to flush the pipes and I have tried using the sys.stdout.flush(). What am I doing wrong? Is there an easier way to setup inter-process communication through Twisted? Thanks, Jon from twisted.internet import reactor, protocol import threading from twisted.python import threadable import os import sys import time threadable.init( with_threads=1) 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() class ReactorFacade: def __init__( self ): self.reactorThread = threading.Thread( target=reactor.run, name="ReactorThread", kwargs={'installSignalHandlers':0}, ) def start(self): # assume not yet started print "Facade: starting..." self.reactorThread.start() print "Facade: started ok" def stop(self): # assume already running print "Facade: request stop" reactor.callFromThread(reactor.stop) print "Facade: request stopped ok" self.reactorThread.join() 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) rf = ReactorFacade() rf.start() time.sleep(3) #If I remove this sleep the following write will register with my process. processProtocol.transport.write( "some command \r\n" ) time.sleep(3) processProtocol.transport.write( "some command \r\n" ) rf.stop()
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
participants (2)
-
jmbenski@micron.com
-
Jp Calderone