如何将spawnProcess的stdout连接到另一个spwn的spawnProcess的stdin,其中带有stdout的spawnProcess在客户端和服务器上的stdin spawnprocess? bash中的CLI命令是btrfs send @mysubvol | btrfs receive /some/path/
。使用rfd, wrd = os.pipe()
我设法将process1管道传输到服务器端的process2。 (现在我想将它从客户端传输到服务器而不是)。以下代码显示了在同一侧管道传输的进程:
以下代码
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
def test2():
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/@mySubvol"],env=None, childFDs={0:"w", 1: wfd })
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/subvolContainer/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
os.close(wfd)
reactor.run()
test2()
我试过了:
from twisted.internet.protocol import Protocol, Factory, ClientFactory
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
class EchoClientFactory(ClientFactory):
protocol = Reader
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
f = Factory()
reactor.listenTCP(8000, f)
rfd = os.pipe()
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
reactor.run()
if __name__ == '__main__':
main()
from twisted.internet import reactor
from twisted.internet import protocol
import sys
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class EchoClientFactory(protocol.ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = EchoClientFactory()
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/home/philipp/testEnv/a2a/@a2"], env=None, childFDs={0:"w", 1: wfd })
reactor.connectTCP('localhost', 8000, factory)
os.close(wfd)
reactor.run()
if __name__ == '__main__':
main()
显然我的尝试是错误的,因为服务器不知道客户端的stdout管道,但我不知道如何将客户端的spawnProcess stdout传输到服务器。
更新01:
根据Jean-Paul的回答,我在客户端和服务器上创建了两个协议(每边ProcessProtocol
和TCP-Protocol
)。我可以成功地将快照从客户端发送到服务器。在客户端,我必须使用ProcessProtocol
的实例启动TCP-Protocol
,以便它们互相连接。请参阅:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol
from twisted.internet import reactor
import sys
import os
class Writer(Protocol):
def connectionMade(self): # Called when a connection is made
print "connec made"
proc = MyProcessProtocol(self)
p1 = reactor.spawnProcess(proc, "btrfs", ["btrfs", "send", "/home/user/testEnv/a2a/@a2"])
class EchoClientFactory(ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
class MyProcessProtocol(ProcessProtocol):
def __init__(self, instance):
self.w = instance
def outReceived(self, data): # Some data was received from stdout
self.w.transport.write(data) # Write some data to the physical connection, in sequence, in a non-blocking fashion
def main():
factory = EchoClientFactory()
reactor.connectTCP('localhost', 8000, factory)
reactor.run()
if __name__ == '__main__':
main()
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol, ServerFactory
from twisted.internet import reactor
import os
class Reader(Protocol):
def connectionMade(self):
print "connected"
self.r2 = Reader2()
p1 = reactor.spawnProcess(self.r2, "btrfs", ["btrfs", "receive", "/"])
def dataReceived(self, data):
print "dataReceived"
self.r2.transport.write(data)
class Reader2(ProcessProtocol):
def connectionMade(self):
print "connectionMade!"
def processEnded(self, reason):
print "quitting"
def main():
f = ServerFactory()
f.protocol = Reader
reactor.listenTCP(8000, f)
reactor.run()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
您无法跨机器设置管道。你也不能在shell中这样做。 shell表达式:
btrfs发送@mysubvol | btrfs接收/ some / path /
在单个计算机上运行两个btrfs
进程,并使用管道连接它们。
管道纯粹是本地的。它们不能跨机器共享。为此,你需要别的东西。例如,TCP连接。
你朝着正确的方向迈出了几步。您的服务器启动TCP服务器。您的客户端尝试与该服务器建立新连接。
但是您的服务器没有定义处理它接受的连接的任何行为。并且您的客户端使用ProcessProtocol
来定义其行为 - 当TCP连接不是进程时。
您可能错过的一个想法是,在您的客户端上,您需要两个协议。一个协议连接到btrfs send
进程并读取其标准输出。另一个协议连接到您的服务器,可以将btrfs send
输出写入TCP连接。
您的服务器也需要两个协议。其中一个处理来自客户端的连接,并读取客户端写入该连接的数据。另一个连接到btrfs receive
进程,并将从TCP连接读取的数据写入该进程的stdin。
您已找到childFDs
的{{1}}功能,这是一个不错的本地优化。虽然您可以在技术上使用它来帮助您在两台不同的机器上连接两个进程,但它涉及您可能不想打扰的额外步骤(至少在您对“常规”转发器感到满意之前)。 / p>
相反,您只需要一些协议,这些协议从靠近设置发送方的对象接收数据(通过childDataReceived和dataReceived),并将其交给设置接收方附近的对象(通过transport.write)。