RPyC で 2 つのクライアントと 1 つのサーバーを接続したいのですが、サーバーのメソッドでクライアント 2 のメソッドを呼び出す client1 からサーバーのメソッドを呼び出したいのですが、これが私のコードです。
import rpyc
#server:
class ServerService(rpyc.Service):
def on_connect(self):
print "Connected To Server\n"
def on_disconnect(self):
print "Disconnected From Server\n"
def exposed_command(self, cmd):
self._cmd = cmd
self._conn.root.run_command(self._cmd)
#client1:
class AppService(rpyc.Service):
def exposed_foo():
return "foo"
conn = rpyc.connect("localhost", 2014, service = AppService)
conn.root.command(self._cmd)
#client2:
class ClientService(rpyc.Service):
def exposed_run_command(self, cmd):
eval(cmd)
# connect to server
conn = rpyc.connect("localhost", 2014, service = ClientService)
サーバーごとに2つのクライアントを接続したい3つの別々のファイルがあります。
アップデート
私は自分の状況をもっと説明し、私が使用している 3 つのモジュールのコードを追加しようとしています...もっと説明して、クライアントの ID についてアドバイスをください。 、1 つのクライアントが Maya Python コマンドを取得する単純な PyQt プログラムを実行し、そのボタンをクリックして、Maya で実行されるクライアント 2 でコマンドを実行したいのですが、わかりましたか? しかし、私は両方のクライアントを一緒に接続し、ピアツーピア接続として相互にメソッドを呼び出したいと考えています。しかし、クライアント1(PyQt)からクライアント2(maya)のrun_commandを呼び出す方法がわかりません。
サーバ側:
import rpyc
class ServerService(rpyc.Service):
def on_connect(self):
print "Connected To Server\n"
def on_disconnect(self):
print "Disconnected From Server\n"
def exposed_command(self, cmd):
self._cmd = cmd
self._conn.root.run_command(self._cmd)
if __name__ == "__main__":
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(ServerService, port = 2014)
print "Server is ready ..."
t.start()
Client1 (PyQt) :
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import rpyc
class ClientApp(QDialog):
def __init__(self, parent = None):
super(ClientApp, self).__init__(parent)
self.label = QLabel("CMD: ")
self.textBox = QLineEdit()
self.button = QPushButton("Run Command")
layout = QHBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.textBox)
layout.addWidget(self.button)
self.setWindowTitle("Client App")
# SIGNALS
self.button.clicked.connect(self.sendCommand)
self._cmd = str(self.textBox.text())
self.sendCommand()
def sendCommand(self):
print "Printed Command : "
self._cmd = str(self.textBox.text())
conn.root.command(self._cmd)
class AppService(rpyc.Service):
def exposed_foo2():
return "foo2"
conn = rpyc.connect("localhost", 2014, service = AppService)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ClientApp()
window.show()
クライアント 2 (マヤ):
import rpyc
from maya import cmds
class ClientService(rpyc.Service):
def exposed_run_command(self, cmd):
eval(cmd)
# connect to server
conn = rpyc.connect("localhost", 2014, service = ClientService)