Summary of commonly used asynchronous frameworks in python

  • 2021-11-13 02:05:43
  • OfStack

Directory body begins
1. Tornado
2. Aiohttp
3.Sanic
4. FastAPI
5. Ruia
Summarize
References

Beginning of the text

asyncio is a standard library introduced in version 3.4 of Python, with direct built-in support for asynchronous IO.

asyncio maintains the EventLoop queue within a single thread, and then adds tasks that need to perform asynchronous IO to the EventLoop queue. As for the completion of tasks, subsequent tasks are realized through callback-like logic. If you have the basics of JavaScript, it is easy to understand asyncio of python, and the keywords, syntax and implementation principles are very similar.


import asyncio

async def main():
    print('Hello ...')
    await asyncio.sleep(1)
    print('... World!')

# Python 3.7+
asyncio.run(main())

1. Tornado

Tornado is an Python web framework and asynchronous network library originally developed by FriendFeed. By using the non-blocking network I/O, Tornado can support tens of thousands of connections and handle long connections, WebSockets, and other applications that require long-term connections with each user.

The official demo is posted below:


import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

2. Aiohttp

An asynchronous web framework based on asyncio supports websocket, does not need to write back the code, has rich ecology, middle price, etc., and is out-of-the-box server and client.

The official demo is posted below:


#  Customer service code 
import aiohttp
import asyncio

async def main():

    async with aiohttp.ClientSession() as session:
        async with session.get('http://python.org') as response:

            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[:15], "...")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

#  Server-side code 
from aiohttp import web

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

async def wshandle(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        if msg.type == web.WSMsgType.text:
            await ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.WSMsgType.binary:
            await ws.send_bytes(msg.data)
        elif msg.type == web.WSMsgType.close:
            break

    return ws


app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/echo', wshandle),
                web.get('/{name}', handle)])

if __name__ == '__main__':
    web.run_app(app)

Ecology of aiohttp:

aiohttp-session provides sessions support for aiohttp services and supports data persistence databases. aiohttp-debugtoolbar provides debugging tools for aiohttp (logging stack information for asyncio exceptions). aiohttp-security provides authentication and rights-related plug-ins for aiohttp. aiohttp-devtools aiohttp development tools, providing development environment deployment, static resources proxy. aiohttp-cors CORS cross-domain authentication support. aiohttp-sse server-side event support (a service for pushing messages on the server side). Support for the pytest-aiohttp pytest test framework. aiohttp-mako Mako server-side template rendering support. aiohttp-jinja2 Jinja2 server-side template rendering support (the famous flask rendering engine). Service tracking in aiozipkin distributed system provides data support for delay problem in micro-service.

aiohttp database support:

aiopg PostgreSQL asynchronous support.
aiomysql MySql asynchronous support.
aioredis Redis asynchronous support.
The other one of asyncpg supports PostgreSQL asynchronously, which is more efficient than aiopg, but api is not universal.

3.Sanic

Sanic is an asyncio-based asyncio server and web framework based on Python 3.7 +. The goal is to provide a simple way to start and run an HTTP server that is easy to build, scale and ultimately perform. It is an asynchronous web framework similar to falsk.

To provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.

Official demo:


from sanic import Sanic
from sanic.response import json

app = Sanic("My Hello, world app")

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run()

4. FastAPI

FastAPI is a high performance web framework for building API, based on Python 3.6 + and supporting standard Python type hints. At the same time, it is the fastest Python web framework, which can be compared with NodeJS and Go (mainly attributed to Starlette and Pydantic).


from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

#  Start  uvicorn main:app --reload
# pip install uvicorn[standard]

5. Ruia

An asynchronous crawler framework based on asyncio and aiohttp, the goal is to allow developers to write crawlers as easily and quickly as possible. Chinese document development support, convenient and fast construction crawler project, custom HTML parsing tools, quick access to page data.

Official demo:


import asyncio

from ruia import Item, TextField, AttrField


class HackerNewsItem(Item):
    target_item = TextField(css_select='tr.athing')
    title = TextField(css_select='a.storylink')
    url = AttrField(css_select='a.storylink', attr='href')


async def test_item():
    url = 'https://news.ycombinator.com/news?p=1'
    async for item in HackerNewsItem.get_items(url=url):
        print('{}: {}'.format(item.title, item.url))


if __name__ == '__main__':
    # Python 3.7 Required.
    asyncio.run(test_item()) 

    # For Python 3.6
    # loop = asyncio.get_event_loop()
    # loop.run_until_complete(test_item())

Summarize

As the python community becomes more and more friendly to asynchronous support, the ecosystem of asynchronous framework is becoming more and more perfect. Tornado is the first one-step framework I came into contact with. Now, with the dispute over the fastest python web framework, Tornado has gradually fallen into the altar. But it doesn't matter who is the fastest, what matters is ecology, and it is important to avoid repeated wheel building.

PS:

I remember when I learned about Sanic, it was based on web framework developed by aiohttp. Now, most of the code has been reconstructed, and the core components have been implemented by myself. Although the syntax of tornado is outdated, it should be the most mature, earliest and documented asynchronous framework. Before using tornado, it was necessary to build wheels for asynchronous operation, but now the ecology is becoming more and more perfect.

Finally, if you want to use an asynchronous framework, remember that all IO operations need to be implemented asynchronously, otherwise it will greatly affect performance. (For example, the third party's SMS service cannot directly use sdk with synchronization code)

References

Aiohttp docs

Sanic Chinese

Uvloop

Tornado Chinese

Above is python commonly used asynchronous framework summary collation details, more information about python asynchronous framework summary please pay attention to other related articles on this site!


Related articles: