{"id":22157435,"url":"https://github.com/laiyongtao/aio_nameko_proxy","last_synced_at":"2025-07-26T08:31:48.102Z","repository":{"id":47067000,"uuid":"257519181","full_name":"laiyongtao/aio_nameko_proxy","owner":"laiyongtao","description":"A standalone nameko rpc proxy for asyncio and some wrappers for using nameko rpc proxy with asynchronous web frameworks(Sanic, fastapi).","archived":false,"fork":false,"pushed_at":"2021-10-19T15:29:50.000Z","size":62,"stargazers_count":17,"open_issues_count":4,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-24T18:01:47.783Z","etag":null,"topics":["asyncio","fastapi","nameko","sanic"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laiyongtao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-21T07:47:04.000Z","updated_at":"2024-02-10T08:10:37.000Z","dependencies_parsed_at":"2022-09-26T21:41:14.454Z","dependency_job_id":null,"html_url":"https://github.com/laiyongtao/aio_nameko_proxy","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laiyongtao%2Faio_nameko_proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laiyongtao%2Faio_nameko_proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laiyongtao%2Faio_nameko_proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laiyongtao%2Faio_nameko_proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laiyongtao","download_url":"https://codeload.github.com/laiyongtao/aio_nameko_proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227333215,"owners_count":17766208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","fastapi","nameko","sanic"],"created_at":"2024-12-02T03:08:50.843Z","updated_at":"2024-12-02T03:08:51.501Z","avatar_url":"https://github.com/laiyongtao.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aio-nameko-proxy\n\nA standalone nameko rpc proxy for asyncio and some wrappers for using nameko rpc proxy with asynchronous web frameworks(Sanic, fastapi). \n\nThis project is based on aio-pika and reference the source code of official nameko project and aio-pika.\n### install\n```shell\npip install aio-nameko-proxy\n```\n### examples:\n#### standalone AIOClusterRpcProxy\nIf you want most of your messages to be persistent(default). Set the delivery mode parameter as\nDeliveryMode.PERSISTENT, Call sw_dlm_call when you need to send a non-persistent message.\n```python\nimport ssl\nimport asyncio\nfrom aio_nameko_proxy import AIOClusterRpcProxy\nfrom aio_pika import DeliveryMode\n\nconfig = {\n    \"AMQP_URI\": \"amqp://guest:guest@127.0.0.1:5672\",  # Required, \n    \"rpc_exchange\": \"nameko-rpc\",\n    \"time_out\": 30, \n    \"con_time_out\": 5, \n    \"delivery_mode\": DeliveryMode.PERSISTENT,\n    \"serializer\": \"my_serializer\",\n    \"ACCEPT\": [\"pickle\", \"json\", \"my_serializer\"],\n    \"SERIALIZERS\": {\n        \"my_serializer\": {\n            \"encoder\": \"my_slizer.dumps\",\n            \"decoder\": \"my_slizer.loads\",\n            \"content_type\": \"my-content-type\",\n            \"content_encoding\": \"utf-8\"\n        }\n    },\n    # If SSL is configured, Remember to change the URI to TLS port. eg: \"amqps://guest:guest@127.0.0.1:5671\"\n    \"AMQP_SSL\": {\n        'ca_certs': 'certs/ca_certificate.pem',  # or 'cafile': 'certs/ca_certificate.pem',\n        'certfile': 'certs/client_certificate.pem',\n        'keyfile': 'certs/client_key.pem',\n        'cert_reqs': ssl.CERT_REQUIRED\n    }\n}\n\nasync def run():\n\n    async with AIOClusterRpcProxy(config) as rpc:\n            # time_out: the time_out of waitting the remote method result.\n            # con_time_out: the time_out of connecting to the rabbitmq server or binding the queue, consume and so on.\n\n            # persistent msg call\n            result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n    \n            reply_obj = await rpc.rpc_demo_service.normal_rpc.call_async(\"demo\")\n            result = await reply_obj.result()\n    \n            # non-persistent msg call\n            result = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call(\"demo\")\n    \n            reply_obj = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call_async(\"demo\")\n            result = await reply_obj.result()\n\n\nif __name__ == '__main__':\n\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(run())\n```\n\nIf you want most of your messages to be non-persistent(persistent is default). Set the delivery mode parameter as\nDeliveryMode.NOT_PERSISTENT, Call sw_dlm_call when you need to send a persistent message.\n```python\nimport asyncio\nfrom aio_nameko_proxy import AIOClusterRpcProxy\nfrom aio_pika import DeliveryMode\nconfig = {\n    \"AMQP_URI\": \"pyamqp://guest:guest@127.0.0.1:5672\",\n    \"rpc_exchange\": \"nameko-rpc\",\n    \"time_out\": 30, \n    \"con_time_out\": 5, \n    \"delivery_mode\": DeliveryMode.NOT_PERSISTENT\n}\n\nasync def run():\n    async with AIOClusterRpcProxy(config) as rpc:\n            # non-persistent msg call\n            result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n    \n            reply_obj = await rpc.rpc_demo_service.normal_rpc.call_async(\"demo\")\n            result = await reply_obj.result()\n    \n            # persistent msg call\n            result = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call(\"demo\")\n    \n            reply_obj = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call_async(\"demo\")\n            result = await reply_obj.result()\n\n\nif __name__ == '__main__':\n\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(run())\n```\n#### AIOPooledClusterRpcProxy\n```python\nimport asyncio\nfrom aio_nameko_proxy import AIOPooledClusterRpcProxy\nfrom aio_pika import DeliveryMode\n\nconfig = {\n    \"AMQP_URI\": \"pyamqp://guest:guest@127.0.0.1:5672\",\n    \"rpc_exchange\": \"nameko-rpc\",\n    \"time_out\": 30, \n    \"con_time_out\": 5,\n    \"pool_size\": 10,\n    \"initial_size\": 2,\n    \"delivery_mode\": DeliveryMode.NOT_PERSISTENT\n}\n\n\nasync def run():\n\n    async with AIOPooledClusterRpcProxy(config) as proxy_pool:\n    \n            async with proxy_pool.acquire() as rpc:\n                result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n\n\nif __name__ == '__main__':\n\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(run())\n```\n\n#### Sanic Wrapper\n```python\nimport ssl\nfrom sanic import Sanic\nfrom sanic.response import json\nfrom aio_pika import DeliveryMode\nfrom aio_nameko_proxy.wrappers import SanicNamekoClusterRpcProxy\n\nclass Config(object):\n    # AMQP_URI: Required\n    NAMEKO_AMQP_URI = \"pyamqp://guest:guest@127.0.0.1:5672\"\n    # rpc_exchange\n    NAMEKO_RPC_EXCHANGE = \"nameko-rpc\"\n    # pool_size\n    NAMEKO_POOL_SIZE = 60\n    # initial_size\n    NAMEKO_INITIAL_SIZE = 60\n    # time_out\n    NAMEKO_TIME_OUT = 30\n    # con_time_out\n    NAMEKO_CON_TIME_OUT = 5\n    # serializer\n    NAMEKO_SERIALIZER = \"json\"\n    # ACCEPT\n    NAMEKO_ACCEPT = [\"pickle\", \"json\"]\n    # SERIALIZERS: custom serializers\n    NAMEKO_SERIALIZERS = {\n        \"my_serializer\": {\n            \"encoder\": \"my_slizer.dumps\",\n            \"decoder\": \"my_slizer.loads\",\n            \"content_type\": \"my-content-type\",\n            \"content_encoding\": \"utf-8\"\n        }\n    }\n    # AMQP_SSL: ssl configs\n    NAMEKO_AMQP_SSL = {\n        'ca_certs': 'certs/ca_certificate.pem',  # or 'cafile': 'certs/ca_certificate.pem',\n        'certfile': 'certs/client_certificate.pem',\n        'keyfile': 'certs/client_key.pem',\n        'cert_reqs': ssl.CERT_REQUIRED\n    }\n    # delivery_mode\n    NAMEKO_DELIVERY_MODE = DeliveryMode.PERSISTENT\n    # other supported properties of aio-pika.Message, the key name format is \"NAMEKO_{}\".format(property_name.upper())\n    # ...\n\n\napp = Sanic(\"App Name\")\napp.config.from_object(Config)\n\n# rpc_cluster = SanicNamekoClusterRpcProxy(app)\n\n# or\n\n# from aio_nameko_proxy.wrappers import rpc_cluster  # contextvars required in py36\n# SanicNamekoClusterRpcProxy(app)\n\n# or\nrpc_cluster = SanicNamekoClusterRpcProxy()\nrpc_cluster.init_app(app)\n\n\n@app.route(\"/\")\nasync def test(request):\n    \n    rpc = await rpc_cluster.get_proxy()\n\n    result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n\n    reply_obj = await rpc.rpc_demo_service.normal_rpc.call_async(\"demo\")\n    result = await reply_obj.result()\n\n    result = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call(\"demo\")\n\n    reply_obj = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call_async(\"demo\")\n    result = await reply_obj.result()\n\n    return json({\"hello\": \"world\"})\n\n\n@app.websocket('/ws')\nasync def ws(request, ws):\n    \n    rpc = await rpc_cluster.get_proxy()\n    \n    for i in range(3):\n        _ = await ws.recv()\n        result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n        await ws.send(result)\n    ws.close()\n    \n    # in websocket handlers, you should call the remove actively in the end\n    rpc_cluster.remove()\n\n\n\nif __name__ == \"__main__\":\n    app.run(host=\"0.0.0.0\", port=8000)\n```\n\n#### FastAPI Wrapper\n```python\nimport ssl\nfrom fastapi import FastAPI, WebSocket\nfrom aio_pika import DeliveryMode\nfrom pydantic import BaseSettings\n\nfrom aio_nameko_proxy.wrappers import FastApiNamekoProxyMiddleware, rpc_cluster  # contextvars required in py36\n\n\n\nclass Settings(BaseSettings):\n\n    # AMQP_URI: Required\n    NAMEKO_AMQP_URI = \"pyamqp://guest:guest@127.0.0.1:5672\"\n    # rpc_exchange\n    NAMEKO_RPC_EXCHANGE = \"nameko-rpc\"\n    # pool_size\n    NAMEKO_POOL_SIZE = 60\n    # initial_size\n    NAMEKO_INITIAL_SIZE = 60\n    # time_out\n    NAMEKO_TIME_OUT = 30\n    # con_time_out\n    NAMEKO_CON_TIME_OUT = 5\n    # serializer\n    NAMEKO_SERIALIZER = \"json\"\n    # ACCEPT\n    NAMEKO_ACCEPT = [\"pickle\", \"json\"]\n    # SERIALIZERS: custom serializers\n    NAMEKO_SERIALIZERS = {\n        \"my_serializer\": {\n            \"encoder\": \"my_slizer.dumps\",\n            \"decoder\": \"my_slizer.loads\",\n            \"content_type\": \"my-content-type\",\n            \"content_encoding\": \"utf-8\"\n        }\n    }\n    # AMQP_SSL: ssl configs\n    NAMEKO_AMQP_SSL = {\n        'ca_certs': 'certs/ca_certificate.pem',  # or 'cafile': 'certs/ca_certificate.pem',\n        'certfile': 'certs/client_certificate.pem',\n        'keyfile': 'certs/client_key.pem',\n        'cert_reqs': ssl.CERT_REQUIRED\n    }\n    # delivery_mode\n    NAMEKO_DELIVERY_MODE = DeliveryMode.PERSISTENT\n    # other supported properties of aio-pika.Message, the key name format is \"NAMEKO_{}\".format(property_name.upper())\n    # ...\n\nsettings = Settings()\n\napp = FastAPI()\n\napp.add_middleware(FastApiNamekoProxyMiddleware, config=settings)\n\n@app.get(\"/\")\nasync def test():\n    \n    rpc = await rpc_cluster.get_proxy()\n\n    result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n\n    reply_obj = await rpc.rpc_demo_service.normal_rpc.call_async(\"demo\")\n    result = await reply_obj.result()\n\n    result = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call(\"demo\")\n\n    reply_obj = await rpc.rpc_demo_service.normal_rpc.sw_dlm_call_async(\"demo\")\n    result = await reply_obj.result()\n\n    return {\"hello\": \"world\"}\n\n\n@app.websocket(\"/ws\")\nasync def ws(ws: WebSocket):\n    await ws.accept()\n    rpc = await rpc_cluster.get_proxy()\n        \n    for i in range(3):\n        _ = await ws.receive()\n        result = await rpc.rpc_demo_service.normal_rpc(\"demo\")\n        await ws.send(result)\n    ws.close()\n    \n    # in websocket handlers, you should call the remove() actively in the end\n    rpc_cluster.remove()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaiyongtao%2Faio_nameko_proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaiyongtao%2Faio_nameko_proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaiyongtao%2Faio_nameko_proxy/lists"}