[Twisted-Python] simple spawnProcess

jmbenski at micron.com jmbenski at micron.com
Wed Aug 10 08:53:48 MDT 2005


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 at twistedmatrix.com [mailto:twisted-python-bounces at 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 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.

Jp

_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list