Deploying a REST API in Python with an existing asyncio event loop

I would like to add a REST API to my application. I already have UNIX (not REST) โ€‹โ€‹socket listeners using Python asyncio that I would like to keep. Most of the frameworks I found to implement the REST API seem to require starting their own event loop (which conflicts with the asyncio event loop).

What is the best approach / library for combining REST / UNIX socket listeners without having to minimize my own implementation from scratch?

Thanks in advance!

+4
source share
1 answer

, , aiohttp. tuture aiohttp:

import asyncio
from aiohttp import web
import code

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.router.add_get('/', handle)
app.router.add_get('/{name}', handle)

loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, '0.0.0.0', 8080)
srv = loop.run_until_complete(f)

loop.run_forever()
code.interact(local=locals())
+5

Source: https://habr.com/ru/post/1660805/


All Articles