2012-06-06 2 views
2

저는 Twisted를 처음 사용합니다. 최근에이 문제가 발생하여 도움이 필요합니다. 내가 알기 론 트위스트 반응기는 단일 스레드 루프로 작동합니다. 루프에서 입력을 얻기 위해 다른 프로세스를 생성 할 때 (그리고 나중에 결과를 루프로 전달할 때), 입력이 전달되지 않아도 프로세스가 바로 종료됩니다 (아래 코드 참조).Twisted spawnProcess에 의해 생성 된 프로세스를 오랫동안 계속 실행하려면 어떻게해야합니까?

또한 입력이 수신 될 때마다 새로운 프로세스를 생성하는 것과는 반대로 오랜 시간 동안 하나의 생성 된 프로세스를 계속 실행할 수 있습니까?

from sys import executable 
from os import environ 
import os 
from twisted.internet import reactor 
from twisted.internet.protocol import Protocol 
from twisted.internet.protocol import Factory 
from twisted.internet import protocol 

implementation = """\ 
import os 
print "in child", os.getpid() 
""" 

class Echo(Protocol): 
    def dataReceived(self, data): 
     reactor.spawnProcess(pp, executable, [executable, "-c", implementation], env=environ) 
     self.transport.write(data) 

class EchoFactory(Factory): 
    def buildProtocol(self, addr): 
     return Echo() 

class MyPP(protocol.ProcessProtocol): 

    def connectionMade(self): 
     print "connectionMade!" 
    def outReceived(self, data): 
     print "out", data 
    def errReceived(self, data): 
     print "error", data 
    def inConnectionLost(self): 
     print "inConnectionLost! stdin is closed! (we probably did it)" 
    def outConnectionLost(self): 
     print "outConnectionLost! The child closed their stdout!" 
    def errConnectionLost(self): 
     print "errConnectionLost! The child closed their stderr." 
    def processExited(self, reason): 
     print "processExited, status %d" % (reason.value.exitCode,) 
    def processEnded(self, reason): 
     print "processEnded, status %d" % (reason.value.exitCode,) 
     print "quitting" 

reactor.listenTCP(8200, EchoFactory()) 
pp = MyPP() 
print 'in parent', os.getpid() 
reactor.spawnProcess(pp, executable, [executable, "-c", implementation], env=environ) 
reactor.run() 

답변

2

트위스트 때 자식 프로세스의 종료를 제어하지 않습니다

Q, 시간 내 주셔서 감사합니다. 하위 프로세스가이를 담당합니다 (여기에 하나 또는 두 가지 예외가 있음). 자식 프로세스는이 코드를 실행하는 Python 프로그램입니다.

import os 
print "in child", os.getpid() 

자체 PID를 인쇄하자마자 종료됩니다. 이것은 Twisted 또는 다른 방법을 사용하여 실행 여부에 관계없이 작동합니다. 입력을 기다리려면 프로그램에 코드를 추가해야합니다.

+0

들어오는 데이터 스트림을 가져올 수 있으며 파이썬 프로그램에서 처리 한 다음 하위 프로세스를 종료하지 않고 상위로 다시 전달할 수 있습니까? –

+1

예. 그것은 가능합니다. –

+0

내가 어떻게 할 수 있는지 예를 들어 주시겠습니까? 예를 들어, 원자로 루프로 들어가는 것을 표준 입력으로 리디렉션합니다. –

관련 문제