- Asynchronous Python API applications - Playground
- Table of Content (ToC)
- Overview
- Useage
- Installation
Table of contents generated with markdown-toc
That project aims at understanding, by practicing, how the asynchronous part of Python API applications work.
To the best of our knowledge, the only asynchronous framework to power Python applications is Uvicorn, which is also the underlying framework used by FastAPI, itself a nice framework to operate Python-based API applications.
The starting point of this project is a discussion on StackOverflow about the respective performances of asynchronous versus synchronous calls.
It is indeed not enough that FastAPI (and Uvicorn) are asynchronous frameworks,
the developers have to explicitly make their code asynchronous, by using
the await
Python command (and specifying the API endpoint callback functions
with the async
prefix). But not all Python payloads may be made
asynchronous-ready.
This project is an attempt to explore what asynchronous means for API endpoints functions.
- Launch the server
- If Uvicorn and FastAPI are already installed in your
pip
environment (see the installation section below if needed):
- If Uvicorn and FastAPI are already installed in your
$ uvicorn uvicorn-delay:app --port 8001 --reload
INFO: Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
INFO: Started reloader process [21835] using statreload
INFO: Started server process [21870]
INFO: Waiting for application startup.
INFO: Application startup complete.
- With pipenv:
$ pipenv run uvicorn uvicorn-delay:app --port 8001 --reload
...
INFO: Application startup complete.
- From another terminal window/tab, query the API with a client, for instance cURL:
$ curl localhost:8001/delay/0.0/0.0 && echo
{"delays":[0.0,0.0],"total_time_taken_ms":5702,"times_avarage_ms":42.69,"times":[146,70,47,42,44,40,31,30,30,69,71,40,40,40,41,40,41,39,40,40,40,40,39,40,40,49,40,40,40,39,40,39,40,39,39,39,39,39,39,44,58,58,43,41,39,38,38,39,39,38,40,39,39,41,39,39,39,51,54,70,43,40,39,39,39,39,38,38,39,39,39,38,38,39,39,38,39,55,63,43,39,38,39,39,39,39,42,41,40,38,39,39,38,38,38,39,39,38,38,38]}
- Or from a browser with the https://localhost:8001/docs URL:
- If not already done so, install PyEnv. For instance
- Prepare the environment (here, with the Bash as default Shell):
$ git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv
$ cat >> ~/.profile << _EOF
# Python
eval "$(pyenv init --path)"
_EOF
$ cat >> ~/.bashrc << _EOF
# Python
export PATH="\${HOME}/.pyenv/bin:\${PATH}"
. ~/.profile
if command -v pyenv 1>/dev/null 2>&1
then
eval "\$(pyenv init -)"
fi
if command -v pipenv 1>/dev/null 2>&1
then
eval "\$(pipenv --completion)"
# echo
fi
_EOF
$ . ~/.bashrc
- If PyEnv was already cloned a long time ago, think to update the repository from time to time:
$ pushd ~/.pyenv && git pull && popd
- Install a specific version of Python (e.g., as of August 2021, 3.9.6 is the latest stable version):
$ pyenv install 3.9.6 && pyenv global 3.9.6
- If not already done so, install
pip
andpipenv
:
$ pip install -U pip
$ pip install -U pipenv
- Clone this repository and change directory to it:
$ mkdir -p ~/dev/showcase && \
git clone https://github.com/cloud-helpers/uvicorn-playground.git ~/dev/showcase/uvicorn-playground
$ cd ~/dev/showcase/uvicorn-playground
- If not already done so, install a few Python required modules
- Either with
pipenv
(so as to insulate that project from anything else):
- Either with
$ pipenv install
- Or with
pip
directly (which may mess with your Python installation):
$ pip install -r requirements.txt