Chapter 10
Chapter 10
- Hypercorn & Daphne & Gunicorn
- Hypercorn: Uvicorn doesn't supprt HTTP/2, but Hypercorn supports HTTP/2 and HTTP/1 and WebSocket.
- with the user of
aioquiclibrary, there is also an experimental support for HTTP/3 - depends on
trio - install:
pipi install hypercorn - run app:
hypercorn app:app --reload
- with the user of
- Daphne: it was originally developed to power Django. It supports HTTP/2 and WebSocket protocols.
- depends on
asyncloop with a twisted library - run app:
daphne -e ssl:8000:privateKey=privatekey.key:certKey=certificate.pem main:app - HTTP is automatically enabled.
- depends on
-
Gunicorn
- With uvicorn, the FastAPI app is run as a single process. In production, we need some replication of processes.
- Gunicorn is another HTTP application server. Although it is a WSGI-compliant server, its process manager class allows the user to choose which worker process class to use.
- Uvicorn has a Gunicorn-compatible worker class. Gunicorn's
process managerdelegates the requests to the worker class for conversion of incoming data to the ASGI standard and further consumption by the FastAPI app. - run app:
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
-
HTTP/2:
-
better efficiency over HTTP1 because of several improvements:
- 1) it uses the binary transfer protocol
- it employs a multiplexing technique to send multiple streams of data at once over a single TCP connection
- if the client requests for an
index.html, which internally uses an image,logo.png, and a stylesheet,style.css, all the three resources are sent over a single connection rather than three as in the case of HTTP/1
-
HTTPS:
-
one of the important considerations while deploying an API is to ensure that the server accepts only secure requests from the client. For HTTPS, the server needs to have a certificate. We can create a self-signed certificate using the RSA cryptography algo.
-
this will generate two files:
privatekey.keyandcertificate.pem. We can use them as values forkeyfileandcertfilefor Uvicorn and Hypercorn -
Render/Heroku/Cyclic
- Render: render.com (has $0 tier)
- environment: python3
- build command:
pip install -r requirements.txt - start command:
uvicorn app:app -host 0.0.0.0 -port 10000
- Heroku: heroku.com (NO $0 tier anymore)
-
Cyclic: cyclic.sh (No $0 tier)
-
Docker
- building container iamges consisting all the dependencies of the app.
- containers are lightweight as compared to virtual machines (VMs)
|---container 1---|---container 2---|---container 3---| | app A | app B | app C | | libs | libs | libs | |-----------------|-----------------|-----------------| | docker engine layer | |-----------------------------------------------------| | hosting operating system | |-----------------------------------------------------| | hosting machin hardware | |-----------------------------------------------------| - keep your
main.pyfile along with therequirements.txtfile in a folder. Alongside it, save the following text as Dockerfile (this files doesn't have any extension): -
then we can build and run
- build:
docker build -t image1 .(. directory has the DockerFile) - run:
docker run -d --name $namemycontainer -p 8000:8000 image1(this will export the port)
- build:
-
GCP
- A FastAPI can be hosted on the Google Cloud in two ways:
- 1) using the Docker image and deploying it
- 2) host the application code on GCP along with its dependencies and serve the application on the public IP address provided by Google Cloud
- Details about approach (2)
- GCP needs a text file named
app.yaml - After logging in GCP, go to console and create a new project
- Running the Cloud Shell, clone the Github repository to GCP, then run the application the same way your run locally
- Then we want to deploy the application to public access. in cloud shell
- The final step is to deploy: Google Cloud uses the YAML file available in the project directory.
- GCP needs a text file named