zhihu avatar zhihu committed 1d635ad Merge

Merge branch 'master' of bitbucket.org:youngking/starry

Comments (0)

Files changed (3)

examples/simple/client.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from protorpc import messages
+from protorpc import remote
+
+from interface import  (SimpleRequest,
+                         SimpleResponse,
+                         BasicService,
+                        )
+
+def client(name='client', address='tcp://0.0.0.0:5000', service_name='basic_service'):
+    from starry.transport  import TcpTransport
+    basic_service = BasicService.Stub(TcpTransport(name, address, service_name))
+    return basic_service
+
+if __name__ == '__main__':
+    basic_service = client()
+    print basic_service.transport.ping()
+    request = SimpleRequest()
+    request.param1 = 'hello, '
+    import time
+    t1 = time.time()
+    print " --------------  sync test  ----------------"
+    for i in range(10):
+        print i
+        request.param2 = 'world %s'%i
+        print basic_service.test(request)
+    t2 = time.time()
+    print "tested: ", t2 -t1

examples/simple/interface.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from protorpc import messages
+from protorpc import remote
+
+class SimpleRequest(messages.Message):
+  """Simple request message type used for tests."""
+
+  param1 = messages.StringField(1)
+  param2 = messages.StringField(2)
+
+
+class SimpleResponse(messages.Message):
+  """Simple response message type used for tests."""
+  value = messages.StringField(1)
+
+
+class BasicService(remote.Service):
+  """A basic service with decorated remote method."""
+
+  def __init__(self):
+    self.request_ids = []
+
+  @remote.method(SimpleRequest, SimpleResponse)
+  def test(self, request):
+      raise NotImplementedError('Method test is not implemented')

examples/simple/server.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from protorpc import messages
+from protorpc import remote
+import time
+from starry.utils import options
+
+from interface import  (SimpleRequest,
+                         SimpleResponse,
+                         BasicService,
+                        )
+
+class BasicServiceImpl(BasicService):
+
+    def test(self, request):
+        time.sleep(0.1)
+        return SimpleResponse(value = request.param1+request.param2)
+
+
+def create_server(address='tcp://0.0.0.0:5000', service_name='basic_service'):
+    from starry.server import RPCServer
+    rpcserver = RPCServer('rpcserver', address)
+    rpcserver.register_service(BasicServiceImpl(), service_name)
+    return rpcserver
+
+
+if __name__ == '__main__':
+    server = create_server()
+    server.start()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.