zeromq plus json

We’ll use ZeroMQ to take JSON-RPC requests, and process them asynchronously. (The synchronous version is here.) The server should respond to “ping” with “pong”.

Install aiozmq to take requests and jsonrpcserver to process them:

$ pip install aiozmq jsonrpcserver

Create a

import asyncio
import aiozmq
import zmq
from jsonrpcserver import method, async_dispatch as dispatch

async def ping():
    return "pong"

async def main():
    rep = await aiozmq.create_zmq_stream(zmq.REP, bind="tcp://*:5000")
    while True:
        request = await
        response = await dispatch(request[0].decode())

if __name__ == "__main__":

Start the server:

$ python


Use jsonrpcclient to send requests:

$ pip install "jsonrpcclient[pyzmq]"
$ python
>>> from jsonrpcclient.clients.zeromq_client import ZeroMQClient
>>> ZeroMQClient("tcp://localhost:5000").request("ping").data.result