2
我是扭曲的新手。我最近遇到這個問題,希望得到一些幫助。據我所知,雙絞線作爲單線程環路運行。當我產生另一個進程從循環中獲取輸入(並且稍後將結果傳遞迴循環)時,即使沒有任何輸入傳遞給它,該進程也會立即退出(請參閱下面的代碼)。如何保持Twisted spawnProcess長時間運行的進程?
另外,是否有可能讓ONE衍生進程長時間運行,而不是每次接收到輸入時產生新進程?
非常感謝您的時間,
Q
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()
是否有可能有一個數據流進入,由python程序處理,然後傳遞迴父級而不退出子進程? –
是的。這是可能的。 –
你能舉一個我怎麼做的例子嗎?例如,我會將進入反應器循環的內容重定向到stdin。 –