Monday, July 10, 2023

What is unicorn

Uvicorn is an ASGI web server implementation for Python.

Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.

Uvicorn currently supports HTTP/1.1 and WebSockets.

$ pip install uvicorn

$ pip install 'uvicorn[standard]'


This will install uvicorn with "Cython-based" dependencies (where possible) and other "optional extras".


In this context, "Cython-based" means the following:


the event loop uvloop will be installed and used if possible.

uvloop is a fast, drop-in replacement of the built-in asyncio event loop. It is implemented in Cython. Read more here.

The built-in asyncio event loop serves as an easy-to-read reference implementation and is there for easy debugging as it's pure-python based.

the http protocol will be handled by httptools if possible.

Read more about comparison with h11 here.

Moreover, "optional extras" means that:


the websocket protocol will be handled by websockets (should you want to use wsproto you'd need to install it manually) if possible.

the --reload flag in development mode will use watchfiles.

windows users will have colorama installed for the colored logs.

python-dotenv will be installed should you want to use the --env-file option.

PyYAML will be installed to allow you to provide a .yaml file to --log-config, if desired.


async def app(scope, receive, send):

    assert scope['type'] == 'http'


    await send({

        'type': 'http.response.start',

        'status': 200,

        'headers': [

            [b'content-type', b'text/plain'],

        ],

    })

    await send({

        'type': 'http.response.body',

        'body': b'Hello, world!',

    })


To run the server, can do the below 


$ uvicorn example:app



References

https://www.uvicorn.org/

No comments:

Post a Comment