[Twisted-Python] simple spawnProcess

jmbenski at micron.com jmbenski at micron.com
Tue Aug 9 18:34:17 EDT 2005


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()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20050809/40717143/attachment.htm 


More information about the Twisted-Python mailing list