Lectures‎ > ‎

Week07

ZeroRPC and Gevent/Greenlet Example

$ cat hello_server.py 
import zerorpc
import gevent


class HelloRPC(object):

    def __init__(self):
        gevent.spawn(self.controller)

    def controller(self):
        while True:
            print("[Contoller]")
            gevent.sleep(1)

    def hello(self, name):
        print("[Request] hello")
        return "Hello, %s" % name

s = zerorpc.Server(HelloRPC())
print(s.bind("tcp://0.0.0.0:4242"))
s.run()

$ cat hello_client.py 
import zerorpc

c = zerorpc.Client()
c.connect("tcp://127.0.0.1:4242")
print c.hello("RPC")

Comments