aiozmq 最简单使用 ,应该可以用在tornado 同步驱动转异步操作

Posted 74 months ago zeromq zmq async asyncio aiozmq python rpc

服务端


import asyncio
from aiozmq import rpc
import random

class Handler(rpc.AttrHandler):
    @rpc.method
    def remote(self, arg1, arg2):
        asyncio.sleep(random.randint(0,9))
        return arg1 + arg2

@asyncio.coroutine
def go():
    server =  yield from rpc.serve_rpc(Handler(),
                                       bind='ipc://a')
    yield from server.wait_closed()
if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(go())
    print("DONE")

客户端



import asyncio
from aiozmq import rpc

@asyncio.coroutine
def go():
    client = yield from rpc.connect_rpc(connect='ipc://a')
    for a in range(1,50000):
        ret = yield from client.call.remote(a, 2)
        print(ret)

asyncio.get_event_loop().run_until_complete(go())

最简单的demo- - 应该还能结合 zeromq的router 进行分布式多进程 worker

点击评论