def sayHello(self): fName = self.UQueue.LoadString() if not fName or not len(fName): raise Se(12345, 'First name cannot be empty!') lName = self.UQueue.LoadString() res = u'Hello ' + fName + ' ' + lName print(res) return Sb().SaveString(res)
def test_enqueue(aq): print('Going to enqueue 1024 messages ......') idMessage = 0 n = 0 with Sb() as sb: while n < 1024: s = str(n) + ' Object test' m = n % 3 if m == 0: idMessage = idMessage0 elif m == 1: idMessage = idMessage1 else: idMessage = idMessage2 # sleep(0.1) # enqueue two unicode strings and one int if not aq.Enqueue( TEST_QUEUE_KEY, idMessage, sb.SaveString('SampleName').SaveString(s).SaveInt(n)): aq.throw(CAsyncQueue.idEnqueue) sb.Size = 0 #reset buffer content n += 1
except CServerError as ex: # an exception from remote server print(ex) except CSocketError as ex: # a communication error print(ex) except Exception as ex: # invalid parameter, bad de-serialization, and so on print('Unexpected error: ' + str(ex)) print('') print( 'Send requests with inline batching for better network efficiency with fewer trips' ) try: fut0 = hw.sendRequest( hwConst.idSayHello, Sb().SaveString('Hilary').SaveString('Clinton')) fut1 = hw.sendRequest(hwConst.idSleep, Sb().SaveUInt(15000)) fut2 = hw.sendRequest( hwConst.idSayHello, Sb().SaveString('Donald').SaveString('Trump')) fut3 = hw.sendRequest(hwConst.idSayHello, Sb().SaveString('Jack').SaveString('Smith')) # save a complex object that has interface IUSerializer implemented fut4 = hw.sendRequest(hwConst.idEcho, Sb().Save(ms)) # hw.Socket.Cancel() print('All requests are sent to server for processing ......') print(fut0.result().LoadString()) print('Buffer size: ' + str(fut1.result().Size)) # sleep returns an empty buffer # bad_operation = fut1.result().LoadString()
def echo(self): # return self.UQueue #this also works ms = CMyStruct() ms.LoadFrom(self.UQueue) return ms.SaveTo(Sb())
def echo(self, ms): return self.sendRequest(hwConst.idEcho, Sb().Save(ms)).result().Load(CMyStruct())
def sleep(self, ms): sb = self.sendRequest(hwConst.idSleep, Sb().SaveInt(ms)).result()
def say_hello(self, fName, lName): buffer = Sb() buffer.SaveString(fName).SaveString(lName) future = self.sendRequest(hwConst.idSayHello, buffer) sb = future.result() return sb.LoadString()
from spa.clientside import * from consts import hwConst from msstruct import CMyStruct class CHelloWorld(CAsyncServiceHandler): def __init__(self): super(CHelloWorld, self).__init__(hwConst.sidHelloWorld) if CUQueue.DEFAULT_OS == tagOperationSystem.osWin: CClientSocket.QueueConfigure.WorkDirectory = "c:\\sp_test" else: CClientSocket.QueueConfigure.WorkDirectory = "/home/yye/sp_test/" CClientSocket.QueueConfigure.MessageQueuePassword = "******" rs = ReplicationSetting() with CReplication(CHelloWorld, rs) as rep: ConnQueue = {} cc = CConnectionContext("127.0.0.1", 20901, "replication", "p4localhost") ConnQueue["Tolocal"] = cc cc = CConnectionContext("192.168.1.122", 20901, "remote_rep", "PassOne") ConnQueue["ToLinux"] = cc ok = rep.Start(ConnQueue, "hw_root_queue_name") ok = rep.StartJob() ok = rep.Send(hwConst.idSayHelloHelloWorld, Sb().SaveString(u'Jack').SaveString(u'Smith')) ok = rep.Send(hwConst.idEchoHelloWorld, Sb().Save(CMyStruct.MakeOne())) ok = rep.EndJob() print('Read a line to continue ......') line = sys.stdin.readline()
super(Pi, self).__init__(piConst.sidPi) print('Client: tell me load balance host address:') cc = CConnectionContext(sys.stdin.readline().strip(), 20901, "lb_client", "pwd_lb_client") with CSocketPool(Pi) as spPi: spPi.QueueName = 'lbqname' if spPi.StartSocketPool(cc, 1): pi = spPi.SeekByQueue() dPi = 0.0 nDivision = 1000 nNum = 10000000 dStep = 1.0 / nNum / nDivision vF = [] for n in range(0, nDivision): dStart = float(n) / nDivision vF.append( pi.sendRequest( piConst.idComputePi, Sb().SaveDouble(dStart).SaveDouble(dStep).SaveInt(nNum))) for f in vF: sb = f.result() dPi += sb.LoadDouble() print('dStart: ' + str(sb.LoadDouble())) print('pi: ' + str(dPi) + ', returns: ' + str(len(vF))) else: print('No connection to ' + cc.Host) print('Press key ENTER to kill the demo ......') sys.stdin.readline()