class Window(QtGui.QMainWindow): remoteCallFinished = QtCore.pyqtSignal(RpcResponse) def __init__(self): super(Window, self).__init__() widget = QtGui.QWidget(self) layout = QtGui.QVBoxLayout(widget) self.button = QtGui.QPushButton("Send command", widget) layout.addWidget(self.button) self.textEdit = QtGui.QPlainTextEdit("Ready to send remote command...", widget) self.textEdit.setReadOnly(True) layout.addWidget(self.textEdit) self.setCentralWidget(widget) self.resize(640, 480) self.setWindowTitle("Client Application") self._remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000") self.remoteCallFinished.connect(self.handleResponse) self.button.clicked.connect(self.sendRequest) @QtCore.pyqtSlot(RpcResponse) def handleResponse(self, resp): self.textEdit.appendPlainText(""" Received reply from remote application: Object: %s Status: %s Error: %s Result: %r """ % (str(resp), resp.status, resp.error, resp.result)) def sendRequest(self): self.textEdit.appendPlainText( "\nSending a remote call to myFunction()...") self._remote.call("myFunction", callback=self.remoteCallFinished.emit, args=(1, "a"), kwargs={ 'flag': True, 'option': "blarg" }) self.textEdit.appendPlainText( "\nSending a remote call to noReturn()...") self._remote.call("noReturn", callback=self.remoteCallFinished.emit) self.textEdit.appendPlainText( "\nSending an async remote call to noReturn()...") self._remote.call("noReturn", callback=self.remoteCallFinished.emit, async=True)
class Window(QtGui.QMainWindow): remoteCallFinished = QtCore.pyqtSignal(RpcResponse) def __init__(self): super(Window, self).__init__() widget = QtGui.QWidget(self) layout = QtGui.QVBoxLayout(widget) self.button = QtGui.QPushButton("Send command", widget) layout.addWidget(self.button) self.textEdit = QtGui.QPlainTextEdit("Ready to send remote command...", widget) self.textEdit.setReadOnly(True) layout.addWidget(self.textEdit) self.setCentralWidget(widget) self.resize(640, 480) self.setWindowTitle("Client Application") self._remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000") self.remoteCallFinished.connect(self.handleResponse) self.button.clicked.connect(self.sendRequest) @QtCore.pyqtSlot(RpcResponse) def handleResponse(self, resp): self.textEdit.appendPlainText(""" Received reply from remote application: Object: %s Status: %s Error: %s Result: %r """ % (str(resp), resp.status, resp.error, resp.result)) def sendRequest(self): self.textEdit.appendPlainText("\nSending a remote call to myFunction()...") self._remote.call("myFunction", callback=self.remoteCallFinished.emit, args=(1, "a"), kwargs={'flag':True, 'option':"blarg"}) self.textEdit.appendPlainText("\nSending a remote call to noReturn()...") self._remote.call("noReturn", callback=self.remoteCallFinished.emit) self.textEdit.appendPlainText("\nSending an async remote call to noReturn()...") self._remote.call("noReturn", callback=self.remoteCallFinished.emit, async=True)
if __name__ == "__main__": WORKER_ID = 0 remote = RpcConnection("Server", workers=1) # if the server were using a TCP connection: # remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000 time.sleep(1) counter = 0 while True: print "timestep: ", counter; finished = False while not finished: resp = remote.call("getAct",args=([WORKER_ID])) if(resp.result != -1): finished = True #print "I got the action, let me do some stuff" #time.sleep(0.01) #print "done, I am sending back the obs" finished = False while not finished: #print "hello" obs = 1+0*np.random.randn(10,1) resp = remote.call("setObs",args=([0.1,WORKER_ID])) #resp = remote.call("setObs",args=([obs])) if(resp.result != -1): finished = True #print "I got the obs ack, I can wait back for next action" counter+=1
ASYNC_CALLS = 0 def callback(resp, *args, **kwargs): global ASYNC_CALLS print("Got slow response:", resp.result) ASYNC_CALLS += 1 if __name__ == "__main__": remote = RpcConnection("Server", workers=1) # if the server were using a TCP connection: # remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000") time.sleep(.1) print("Calling slow()") for i in range(5): remote.call("slow", is_async=True, callback=callback) print("Calling fast()") resp = remote.call("fast") print("Got fast response:", resp.result) print("Waiting on async calls to finish") while ASYNC_CALLS < 5: time.sleep(.1)
ASYNC_CALLS = 0 def callback(resp, *args, **kwargs): global ASYNC_CALLS print("Got slow response:", resp.result) ASYNC_CALLS += 1 if __name__ == "__main__": remote = RpcConnection("Server", workers=1) # if the server were using a TCP connection: # remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000") time.sleep(.1) print("Calling slow()") for i in range(5): remote.call("slow", async=True, callback=callback) print("Calling fast()") resp = remote.call("fast") print("Got fast response:", resp.result) print("Waiting on async calls to finish") while ASYNC_CALLS < 5: time.sleep(.1)
ASYNC_CALLS = 0 def callback(resp, *args, **kwargs): global ASYNC_CALLS print "Got slow response:", resp.result ASYNC_CALLS += 1 if __name__ == "__main__": remote = RpcConnection("Server", workers=1) # if the server were using a TCP connection: # remote = RpcConnection("Server", tcpaddr="127.0.0.1:40000") time.sleep(.1) print "Calling slow()" for i in xrange(5): remote.call("slow", async=True, callback=callback) print "Calling fast()" resp = remote.call("fast") print "Got fast response:", resp.result print "Waiting on async calls to finish" while ASYNC_CALLS < 5: time.sleep(.1)