How the duck those packages work?

@marcelotryle

About me

bg:40%

Marcelo Trylesinski

@marcelotryle

FastAPI Expert

<img src="/assets/fastapi_expert.png" style="display: block; margin-left: auto; margin-right: auto; border-radius: 5px; width:80%" />
@marcelotryle

OSS Maintainer

<div class="columns"> <div>

Uvicorn

</div> <div>

Starlette

</div> </div>
@marcelotryle

Software Engineer at Pydantic

@marcelotryle

Motivation

@marcelotryle

Understand how things are connected

This is the goal for today!

@marcelotryle

@marcelotryle

Who is the server?

@marcelotryle

Let's learn how things happen!

@marcelotryle

Client initiates the connection

@marcelotryle

Server creates a task (connection)

@marcelotryle

Uvicorn implements asyncio.Protocol

@marcelotryle

connection_made is called

https://docs.python.org/3/library/asyncio-protocol.html#asyncio.BaseProtocol.connection_made

@marcelotryle

data_receive is called

https://docs.python.org/3/library/asyncio-protocol.html#asyncio.Protocol.data_received

@marcelotryle

h11 is a Sans-IO protocol library

httptools is a callback based library

@marcelotryle

Server will receive the request

@marcelotryle

Two things can happen

1. Create the "HTTP connection Scope"

2. Upgrade the connection

@marcelotryle

Create the "HTTP connection Scope"

headers, path, method, query_string, etc.

https://asgi.readthedocs.io/en/latest/specs/www.html#http-connection-scope

@marcelotryle

Or... Server will upgrade the connection

@marcelotryle

Server will run the application

@marcelotryle

Server will communicate with the application via ASGI events

@marcelotryle

What's ASGI?

https://asgi.readthedocs.io/en/latest/

@marcelotryle

Simple ASGI application

@marcelotryle

Echo ASGI application

@marcelotryle

FastAPI is an ASGI application

@marcelotryle

FastAPI is an ASGI application

@marcelotryle

Starlette is an ASGI application

@marcelotryle

Middleware & ASGI Application

@marcelotryle

Middleware & ASGI Application

@marcelotryle

Starlette does the routing

Starlette -> Router -> Route

@marcelotryle

The endpoint is called

@marcelotryle

Dependency Injection

@marcelotryle

The data is validated

@marcelotryle

What is Pydantic?

@marcelotryle

Come to me to get my card, to get early access to the product we are building!

@marcelotryle

Endpoint function runs

@marcelotryle

Data will be validated on output

@marcelotryle

Middlewares again

Remember?

@marcelotryle

The application sends events to the ASGI server

@marcelotryle

ASGI server makes sure the ASGI application complies to the spec

@marcelotryle

Server sends data to the client

@marcelotryle

What now?

@marcelotryle

Task will stay alive for a while

@marcelotryle

Connection object is closed

@marcelotryle

That's it!

@marcelotryle

Support for WebSockets

@marcelotryle

Support for Lifespan Events

@marcelotryle

What happens on shutdown?

@marcelotryle

Where to go from here?

  1. Help on issues
  2. Watch your favorite packages on GitHub
@marcelotryle

Predicting questions...

Starlette/FastAPI/Uvicorn V1

@marcelotryle

Predicting questions...

Will you come to India if you get invited next year?

@marcelotryle

Yes.

@marcelotryle

Thank You!

FastAPIExpert.com

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<i class="fab fa-linkedin"></i> Marcelo Trylesinski
<i class="fab fa-twitter"></i> @marcelotryle
<i class="fab fa-github"></i> Kludex

@marcelotryle

Reference: https://github.com/koddr/starlette-logo

So... For those who don't know me... I work fully remote, and every day I go to this coffee place that is around 5 minutes bike from my home. And... I asked the barista that works there to go out. We went out, and at some point I told her that "I do Open Source". I explained what open source is... - "I'm kind a communist on the code world." - "People contribute to public projects." - "Everybody use these makes use of those projects." Then I tried to explain specifically what I do in a nice way... I started with: "Yeah, you know... I kind a build the tools that people use to build real stuff" She was a bit confused, and said "I understand, I guess." I don't think she did. We are not going out anymore. Not for those reasons, but... Talking to her gave me the idea for this talk. It made me think about how can I explain in a nice way what I really know to others. Also, this is a subject that really excites me, since it touches a lot of packages in the ecosystem. Besides, I've only met nice people from India. So I dedicated a bit more of myself to this. Hope you enjoy this talk, as much as I enjoy your company.

Talk about Uvicorn (web server), FastAPI (web framework), and the client.

An image of Uvicorn. Also, explain what Uvicorn is.

Uvicorn is a server implementation that runs applications that look like FastAPI.

At the first moment, a 3-way TCP handshake is established, and then maybe a TLS handshake as well.

When I say the server, here I'll always mean Uvicorn.

Creates an object for that connection.

Show the uvicorn code

Explain that TCP handshake, and TLS handshake happens.

The request will be received by the task.

Show a picture of: Request line + Headers + CR line + body

- Request line (method, path, query) - HTTP headers

It's just to create the scope. See how the scope looks like.

Meaning it can be upgraded to WebSockets. Then the WebSocket scope will be created.

Or HTTP/2

Show the `run_asgi` method in Uvicorn.

Each request means that the application is ran. Meaning that `FastAPI().__call__` is called.

Asynchronous Server Gateway Interface

It's the WSGI successor. But... I don't know much about WSGI. I don't have many years of experience.

Depends on the keep alive timeout.

Is there time to talk about it?

Is it even worth it?

Help on issues!

Watch your favorite packages on GitHub.

Things don't happen overnight. It takes time to get really good at something. Be patient.