CS Fundamentals Trainer · Guides
Python for data and web work: containers, pandas, WSGI and ASGI
Which built-in container fits the job and what it costs, how pandas gets its speed and where it surprises you, and how a web request reaches your code through WSGI or ASGI. With measured results: dict lookups, deque against list, records against columns, vectorised against looped pandas, and 20 concurrent requests on sync, threaded and async servers.
22 min read
Most real Python work lands in one of two places: processing data (lists of records, tables, files) or serving requests (an API or a website). The two have the same shape of problem: choose structures and tools whose costs match the size of the job, and know which layer is actually doing the work. This guide covers the built-in containers and what they cost, how pandas gets its speed (and where it surprises you), and how a request reaches your Python code through WSGI or ASGI, with the numbers from scripts run on CPython 3.13 (in the appendix). It builds on how Python works and Python performance and concurrency.
Versions used: pandas 3.0.5, NumPy 2.5.3, Flask 3.1.3, Pydantic 2.13.5, Starlette 1.7.0 and uvicorn 0.54.0, all as installed in the sandbox. Django and FastAPI were not installed, so what I say about them comes from their documentation and not from a run. The machine had two CPU cores and timings vary; the shapes of the results are the point.
1. The built-in containers and their costs
| Container | Good for | Costs |
|---|---|---|
list | ordered items, append at the end, index | x in l scans (O(n)); insert or pop at the front is O(n) |
tuple | fixed records, dictionary keys | immutable; slightly smaller than a list |
dict | lookup by key, records, counting | hash lookup; insertion-ordered; memory heavy |
set | membership, de-duplication, set algebra | hash lookup; no order; memory heavy |
collections.deque | queues (fast at both ends) | slow indexing in the middle |
heapq | the smallest item first, top-k | O(log n) push and pop |
Dictionaries
A dictionary is a hash table: it turns a key into a slot index, so the cost of a lookup does not depend on the number of entries. I measured a lookup of the last key in dictionaries of 1,000, 100,000 and 1,000,000 entries: 55 ns, 45 ns and 47 ns, flat, within noise. Since Python 3.7 a dictionary also keeps insertion order as part of the language: after inserting b, a, c and deleting then re-adding a, the keys were ['b', 'c', 'a'].
The price is memory. A dictionary resizes in steps; the container itself grew from 224 bytes to 352 at the 6th entry, to 632 at the 11th and to 1,168 at the 22nd. At scale, a dictionary of one million integer keys took about 42 MB for the table, a set of the same keys about 34 MB, and a list of one million items about 8 MB (all excluding the integer objects themselves). When you have a million items and only need to iterate, a list or generator is far cheaper than a dictionary or set.
Two rules that cause real bugs: keys must be hashable and their hash must not change (so lists cannot be keys; see how Python works), and you cannot change a dictionary's size while iterating over it: adding keys inside for k in d raised RuntimeError: dictionary changed size during iteration. Iterate over list(d) (a snapshot) when you need to modify it.
Lists as queues
A list is a dynamic array: appending is cheap, but removing from the front shifts every other item. I popped 20,000 items from the front of a 100,000-item list: 329 ms. The same from a collections.deque with popleft(): 0.7 ms, roughly 470 times faster. Use a deque for queues and breadth-first search, and queue.Queue between threads.
Records versus columns
How you shape data changes memory. I stored 200,000 records of three fields two ways:
| Layout | Memory |
|---|---|
a list of 200,000 dictionaries ({"id":…, "name":…, "score":…}) | about 50 MB |
| one dictionary of three lists (columns) | about 16 MB |
Each record dictionary pays for its own table. Storing data by column is both smaller and faster to scan, which is exactly the layout pandas and NumPy use (next sections). For many small records in plain Python, use tuples, NamedTuple or a dataclass(slots=True) (see classes and typing).
Other useful tools: collections.Counter for counting (Counter(["a", "b", "a"]).most_common(1) gave [('a', 2)]), defaultdict to avoid existence checks, sorted(key=...) (stable: equal keys keep their original order), and bisect for sorted lists.
2. Data with pandas
pandas stores a table as columns of NumPy arrays. Operations on a whole column run in compiled code over contiguous memory, which is where the speed comes from. The cost is that code written like ordinary Python loops loses it.
Vectorise
I computed amount * 1.2 for 20,000 rows in four ways:
| Method | Time |
|---|---|
iterrows() (a Python loop that builds a Series for each row) | 410 ms |
itertuples() (a Python loop over tuples) | 14 ms |
Series.apply(lambda v: v * 1.2) (calls a Python function per value) | 2.7 ms |
df["amount"] * 1.2 (vectorised) | 0.06 ms |
That is a factor of about 6,800 between the slowest and the fastest. The vectorised version over all 200,000 rows took 0.31 ms. Rules: never loop over rows to compute a column; use column arithmetic, np.where, .str and .dt accessors, groupby and merge; and when you really need Python per row, prefer itertuples to iterrows. A groupby("country")["amount"].sum() over 200,000 rows took about 15 ms, and grouped 40,000 rows per country into five totals.
Dtypes and memory
Every column has a dtype, and the choice matters:
- Repeated strings (country codes, statuses) as the
categorydtype: my 200,000-rowcountrycolumn used 10.2 MB as strings and 0.2 MB as a category (integer codes plus a small lookup table). - Integers sized to the data: a
usercolumn of numbers up to 999 took 1.6 MB asint64and 0.4 MB asint16. Check the range first; fixed-width integers overflow silently (see the performance guide). - Load only the columns you need (
usecols=), and read big files in chunks:read_csv(..., chunksize=25_000)summed to exactly the same total as reading the whole 100,000-row file at once, with a fraction of the memory at any time. - If it does not fit in memory even so, use a columnar file format (Parquet), a database, or a tool built for larger-than-memory data (Polars, DuckDB, Dask). A SQL aggregation inside the database is often the right answer (database indexes).
Copies, views and missing values
The classic pandas confusion is whether changing a derived table changes the original. In pandas 3, copy-on-write is the behaviour: any table or column derived from another behaves as an independent copy when you modify it. I filtered a table (b = a[a["x"] > 1]) and wrote into b; a stayed [1, 2, 3]. The same held when I changed a Series taken from the original, and an explicit .copy() obviously left it untouched. In older pandas versions, some of these operations modified the original or raised SettingWithCopyWarning; check your version, and write code that does not depend on the difference (assign results with df = df.assign(...) or df.loc[mask, "col"] = value on the table you own).
Missing data has its own rules:
NaN == NaNisFalse; test withisna(). Aggregations such asmeanskipNaNby default ([1.0, NaN, 3.0]gave 2.0).- An integer column with a missing value becomes float:
pd.Series([1, 2, None]).dtypewasfloat64. The nullableInt64dtype keeps integers. - Floating point is inexact:
0.1 + 0.2 == 0.3isFalse, also in a pandas sum. Keep money as integer cents orDecimal.
When pandas is slow, it is almost always because the work happens per row in Python, or because the data type or the file format forced a copy. Profile first (performance guide).
3. How a web request reaches your code
A web application is a Python function (or a few) that a server calls with a description of the request. Python standardised the interface twice.
WSGI: one call per request, synchronous
WSGI (PEP 3333) says an application is a callable taking environ (a dictionary describing the request) and start_response, and returning the body as an iterable of bytes. My whole app was:
def wsgi_app(environ, start_response):
body = f"wsgi {environ['REQUEST_METHOD']} {environ['PATH_INFO']}".encode()
start_response("200 OK", [("Content-Type", "text/plain"),
("Content-Length", str(len(body)))])
return [body]
Frameworks such as Flask and Django are WSGI applications under the hood (Django can also run as ASGI). I called a Flask app's wsgi_app by hand with a handmade environ for POST /orders and got 201 CREATED and the JSON body: the framework is a convenient layer over exactly that call. WSGI servers (gunicorn, uWSGI, waitress) run many copies of it in processes and threads.
ASGI: async, and more than HTTP
ASGI is the asynchronous successor: an application is async def app(scope, receive, send); it can await between receiving and sending, and the same interface handles WebSockets and long-lived connections. Starlette and FastAPI (built on Starlette and Pydantic) are ASGI frameworks; uvicorn and hypercorn are ASGI servers.
What it means for concurrency
I served one endpoint that waits 0.1 s (like a slow database call) through four servers and sent 20 requests at once from 20 client threads:
| Server | 1 request | 20 at once |
|---|---|---|
WSGI, one request at a time (wsgiref) | 103 ms | 2.34 s |
| WSGI, one thread per request | 103 ms | 0.11 s |
ASGI, async handler (uvicorn) | 103 ms | 0.12 s |
ASGI, handler that calls a blocking time.sleep | 102 ms | 2.02 s |
- A single-threaded synchronous server handles requests in line: 20 × 0.1 s plus overhead. Real WSGI servers add worker processes and threads to avoid this, and the count of workers is your concurrency limit.
- An async handler overlapped all 20 waits on one thread.
- The last row is the trap from the performance and async guides: one blocking call inside an async handler stops the whole event loop, turning the async server back into a sequential one. Inside async code use async libraries (database drivers, HTTP clients), or push blocking calls to a thread with
asyncio.to_thread.
Neither style makes CPU-heavy handlers faster. Move heavy computation out of the request path (a task queue and a worker process, see message queues), or run more worker processes.
Frameworks compared
| Flask | Django | FastAPI / Starlette | |
|---|---|---|---|
| Interface | WSGI | WSGI and ASGI | ASGI |
| Style | micro: routing and templates, you choose the rest | batteries included: ORM, migrations, admin, auth | typed endpoints, automatic request validation and OpenAPI docs (FastAPI) |
| Choose when | small services, full control, learning | content-heavy sites, admin interfaces, teams that want conventions | APIs with many async calls, strongly typed input and output |
(Django and FastAPI are described from their documentation; only Flask and Starlette ran here.) The choice matters less than the habits below, which apply to all of them.
4. Habits for web handlers
Validate input at the boundary. Request data is untrusted (see web application security). Pydantic (used by FastAPI, and usable anywhere) describes the shape once:
class NewOrder(BaseModel):
email: str
quantity: int = Field(gt=0, le=100)
@field_validator("email") ... # custom rule: must contain "@", lowercased
With a Flask handler calling NewOrder.model_validate(...), a valid body got 201 and {'email': 'a@b.com', 'quantity': 3} (lowercased by the rule), and {"email": "nope", "quantity": 0} got 422 with a list naming both bad fields (Value error, must contain @ and Input should be greater than 0). Note that by default Pydantic coerces: the string "5" became the integer 5; use strict mode where that is not acceptable. Framework routing also validates: /orders/<int:order_id> returned 404 for /orders/abc before my code ran, a missing path gave 404 and the wrong method gave 405. Use the status codes from the REST API guide.
Do not keep request state in module globals. A global counter lives once per process. I ran a small app under uvicorn with two worker processes and sent 12 concurrent requests: each process counted its own share, so their counters reached about 6 each, never 12 (one run split 7 and 5). Anything shared between workers (sessions, caches, rate-limit counters, job state) belongs in a database or a shared store such as Redis, and the same applies when you scale to several machines (load balancing).
Mind connections. Each worker process has its own database connection pool, so total connections = workers × pool size; a database accepts only so many. Use a pool, release connections promptly (with), and put timeouts on everything that waits (resilience patterns).
Keep handlers short. Do the quick part in the request and queue the rest; return 202 for long jobs. Avoid loading a whole table into pandas inside a request: aggregate in the database.
Deployment shape. Run the application server (gunicorn or uvicorn) behind a reverse proxy such as nginx or a cloud load balancer, which handles TLS, slow clients, static files and buffering (TLS and HTTPS, DNS, HTTP/2, HTTP/3 and CDNs). Never expose the framework's development server to the internet. Pin dependencies (packaging guide), and log requests with an id so a failure can be traced (observability).
5. Common mistakes
- Using a list for membership tests or as a queue (329 ms against 0.7 ms for front pops).
- Modifying a dictionary while iterating over it.
- A list of dictionaries for millions of records (50 MB against 16 MB by column).
- Looping over DataFrame rows (
iterrows: 410 ms against 0.06 ms vectorised), andapplyfor things that have vector forms. - Strings where categories belong (10.2 MB against 0.2 MB), and
int64for small numbers. - Relying on pandas version-specific copy behaviour instead of writing code that is explicit.
- Float money and
==on floats. - Blocking calls inside
async defhandlers (2.02 s against 0.11 s). - State in module globals with more than one worker.
- No validation at the boundary, or trusting coerced types blindly.
- Heavy work in the request path, and one database connection per request without pooling.
- Running the development server in production.
6. Questions interviewers like to ask
"How does a dictionary work, and what is its lookup complexity?"
A hash table: the key's hash picks a slot, so lookup, insert and delete are O(1) on average and do not slow down with size. Keys must be hashable; insertion order is preserved; the memory cost is high.
"Why is a loop over a DataFrame slow?"
Each iteration runs Python bytecode and creates a Python object per row, bypassing the compiled column operations. Use vectorised operations (df["a"] * 1.2, groupby, merge), which run in C over arrays.
"How would you process a file larger than memory?"
Stream or chunk it (chunksize, generators), load only needed columns, use compact dtypes, push aggregation into a database or use a columnar engine (Parquet with DuckDB or Polars).
"What is the difference between WSGI and ASGI?"
WSGI is a synchronous one-call-per-request interface; ASGI is asynchronous, supports await and also WebSockets and other long-lived protocols. Flask and Django are WSGI (Django also ASGI); Starlette and FastAPI are ASGI.
"A handler is slow under load. What do you check?"
Profile where the time goes; look for blocking calls in async code, missing indexes and N+1 queries, connection-pool exhaustion, too few workers, and CPU-bound work that belongs in a queue.
"Why can a global variable in a web app give inconsistent results?"
Each worker process has its own copy, so state diverges between processes (my two workers each counted about half the requests). Use shared storage.
"Flask, Django or FastAPI?"
Flask for small services with freedom of choice; Django for full sites with an ORM, admin and conventions; FastAPI for typed async APIs with automatic validation and documentation. The foundations (HTTP semantics, validation, pools, queues) are the same.
7. Things to mention in an interview
- Container complexities (list, dict, set, deque, heapq) and memory costs; records against columns.
- pandas: columnar storage, vectorisation, dtypes (
category, small ints), chunking, copy semantics, missing-value behaviour. - WSGI against ASGI and what blocks what; worker processes as the concurrency unit; never block the event loop.
- Validation at the boundary (Pydantic), status codes, per-process state, connection pools, queues for slow work.
- Deployment behind a reverse proxy, pinned dependencies, request ids and metrics.
Quick recap
- A dict lookup stayed at about 45–55 ns from 1,000 to 1,000,000 keys, but costs memory (about 42 MB for a million keys); a
dequepopped from the front about 470 times faster than a list (0.7 ms against 329 ms). - Data by column took 16 MB where a list of dicts took 50 MB.
- In pandas, vectorising
amount * 1.2took 0.06 ms for 20,000 rows against 410 ms foriterrows; thecategorydtype cut a string column from 10.2 MB to 0.2 MB. - pandas 3 uses copy-on-write: changing a filtered copy left the original
[1, 2, 3]. Missing integers turn a column into float unless you useInt64. - WSGI is one synchronous call per request; ASGI is async. Twenty requests that wait 0.1 s took 2.34 s on a one-at-a-time server, 0.11–0.12 s with threads or async, and 2.02 s when an async handler blocked.
- Validate input with a schema (422 for bad fields), keep state out of module globals (two workers each counted about half), pool connections, queue slow work, and run behind a reverse proxy.
Appendix: the experiment code
Python 3.13 on a two-core machine, with pandas 3.0, NumPy 2.5, Flask 3.1, Pydantic 2.13, Starlette and uvicorn. Run the files from one folder with python3 file.py; the load test starts and stops its own servers on local ports 8101–8104 and the state demo uses port 8110. Timings vary by machine; pandas results, especially copy behaviour, depend on the version.
containers.py: dictionary growth and lookup, deque against list, and records against columns:
import sys, timeit, random, collections
# dict growth and cost
d = {}
sizes = []
last = None
for i in range(40):
d[i] = i
s = sys.getsizeof(d)
if s != last: sizes.append((len(d), s)); last = s
print("dict resizes (len, bytes):", sizes)
print("1M-entry dict:", f"{sys.getsizeof({i: i for i in range(1_000_000)})/1e6:.0f} MB", "| list of 1M:", f"{sys.getsizeof(list(range(1_000_000)))/1e6:.0f} MB", "| set of 1M:", f"{sys.getsizeof(set(range(1_000_000)))/1e6:.0f} MB (containers only, not the ints)")
# insertion order guaranteed
d = {}; d["b"] = 1; d["a"] = 2; d["c"] = 3; del d["a"]; d["a"] = 9
print("insertion order kept:", list(d))
# lookup cost grows slowly with size
for n in (1_000, 100_000, 1_000_000):
dd = {i: i for i in range(n)}; k = n - 1
print(f"dict lookup, {n:>9,} keys: {timeit.timeit(lambda: dd[k], number=2_000_000)/2e6*1e9:.0f} ns")
# modifying while iterating
d = {i: i for i in range(5)}
try:
for k in d: d[k + 10] = 1
except RuntimeError as e: print("RuntimeError:", e)
# list as queue vs deque
n = 100_000
l = list(range(n)); dq = collections.deque(range(n))
t_l = timeit.timeit(lambda: [l.pop(0) for _ in range(20_000)], number=1)
t_d = timeit.timeit(lambda: [dq.popleft() for _ in range(20_000)], number=1)
print(f"20,000 pops from the front of a {n:,}-item list {t_l*1e3:.0f} ms | deque {t_d*1e3:.1f} ms")
# Counter / defaultdict / sorted with key
words = [random.choice("abcdefghij") * 3 for _ in range(10)]
print(collections.Counter(["a", "b", "a"]).most_common(1), "| sorted stable:", sorted([("b", 1), ("a", 1), ("c", 0)], key=lambda t: t[1]))
# dict vs list of dicts memory for records
rows = 200_000
import tracemalloc
tracemalloc.start(); rec = [{"id": i, "name": "x", "score": i * 0.5} for i in range(rows)]; a, _ = tracemalloc.get_traced_memory(); tracemalloc.stop()
tracemalloc.start(); cols = {"id": list(range(rows)), "name": ["x"] * rows, "score": [i * 0.5 for i in range(rows)]}; b, _ = tracemalloc.get_traced_memory(); tracemalloc.stop()
print(f"{rows:,} records: list of dicts {a/1e6:.0f} MB | dict of columns {b/1e6:.0f} MB")
pandas_demo.py: vectorisation, dtypes and memory, groupby, copy-on-write, missing data and chunked reading:
import time, timeit, warnings
import numpy as np, pandas as pd
print("pandas", pd.__version__, "numpy", np.__version__)
rng = np.random.default_rng(0)
n = 200_000
df = pd.DataFrame({"user": rng.integers(0, 1000, n), "country": rng.choice(["BR", "DE", "US", "FR", "JP"], n), "amount": rng.random(n) * 100})
# 1. three ways to compute a new column
def t(f, k=3): return min(timeit.repeat(f, number=1, repeat=k))
rows = df.head(20_000)
loop = t(lambda: [r.amount * 1.2 for r in rows.itertuples()])
iterrows = t(lambda: [r["amount"] * 1.2 for _, r in rows.iterrows()], 1)
apply_ = t(lambda: rows["amount"].apply(lambda v: v * 1.2))
vec = t(lambda: rows["amount"] * 1.2)
print(f"20,000 rows -> iterrows {iterrows*1e3:.0f} ms | itertuples {loop*1e3:.0f} ms | apply {apply_*1e3:.1f} ms | vectorised {vec*1e3:.2f} ms")
print(f"200,000 rows vectorised: {t(lambda: df['amount'] * 1.2)*1e3:.2f} ms")
# 2. dtypes and memory
print("memory MB, object/str country:", round(df.memory_usage(deep=True)["country"]/1e6, 2), end=" | ")
cat = df["country"].astype("category"); print("category:", round(cat.memory_usage(deep=True)/1e6, 2), "| dtype was:", df["country"].dtype)
small = df["user"].astype("int16"); print("user int64:", round(df["user"].memory_usage(deep=True)/1e6, 2), "MB -> int16:", round(small.memory_usage(deep=True)/1e6, 2), "MB")
# 3. groupby, and the sort/merge costs
g = t(lambda: df.groupby("country")["amount"].sum())
print(f"groupby sum over {n:,} rows: {g*1e3:.1f} ms")
print(df.groupby("country")["amount"].agg(["count", "sum"]).round(1).sort_index().to_string())
# 4. copy semantics (Copy-on-Write is the default in pandas 3)
a = pd.DataFrame({"x": [1, 2, 3]})
b = a[a["x"] > 1]
b.loc[:, "x"] = 99
print("original after changing a filtered copy:", a["x"].tolist())
c = a["x"]; c.iloc[0] = -1
print("original after changing a Series taken from it:", a["x"].tolist())
a2 = a.copy(); a2.loc[0, "x"] = 7; print("explicit copy leaves original:", a["x"].tolist())
# 5. NaN and missing data
s = pd.Series([1.0, np.nan, 3.0]); print("NaN == NaN:", np.nan == np.nan, "| mean skips NaN:", s.mean(), "| sum of ints with None -> dtype:", pd.Series([1, None, 3]).dtype)
print("int column with a missing value becomes:", pd.Series([1, 2, None]).dtype, "| nullable Int64:", pd.Series([1, 2, None], dtype="Int64").dtype)
# 6. float money
print("0.1+0.2 sum in pandas:", pd.Series([0.1, 0.2]).sum() == 0.3, "| round-trip cents as ints:", (pd.Series([10, 20]).sum() == 30))
# 7. reading a big CSV in chunks
import io
csv = "id,v\n" + "\n".join(f"{i},{i%7}" for i in range(100_000))
total = 0
for chunk in pd.read_csv(io.StringIO(csv), chunksize=25_000): total += chunk["v"].sum()
print("chunked sum equals whole:", total == pd.read_csv(io.StringIO(csv))["v"].sum())
apps.py: the same slow endpoint as WSGI and ASGI apps:
"""The same endpoint (sleeps 0.1 s, like a slow database call) as WSGI and ASGI apps."""
import asyncio, time
def wsgi_app(environ, start_response): # WSGI: a plain function called once per request
time.sleep(0.1)
body = f"wsgi {environ['REQUEST_METHOD']} {environ['PATH_INFO']}".encode()
start_response("200 OK", [("Content-Type", "text/plain"), ("Content-Length", str(len(body)))])
return [body]
async def asgi_async(scope, receive, send): # ASGI: an async function with three arguments
assert scope["type"] == "http"
await asyncio.sleep(0.1) # waits without blocking the event loop
await send({"type": "http.response.start", "status": 200, "headers": [(b"content-type", b"text/plain")]})
await send({"type": "http.response.body", "body": f"asgi {scope['method']} {scope['path']}".encode()})
async def asgi_blocking(scope, receive, send): # the classic mistake: a blocking call inside async code
time.sleep(0.1)
await send({"type": "http.response.start", "status": 200, "headers": [(b"content-type", b"text/plain")]})
await send({"type": "http.response.body", "body": b"blocking"})
serve_wsgiref.py: the WSGI app on a one-at-a-time server and a threaded one:
import sys
from socketserver import ThreadingMixIn
from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
from apps import wsgi_app
class Quiet(WSGIRequestHandler):
def log_message(self, *a): pass
class Threaded(ThreadingMixIn, WSGIServer): daemon_threads = True
port, threaded = int(sys.argv[1]), sys.argv[2] == "threaded"
make_server("127.0.0.1", port, wsgi_app, server_class=Threaded if threaded else WSGIServer, handler_class=Quiet).serve_forever()
loadtest.py: starts the four servers in turn and sends 20 simultaneous requests to each:
import subprocess, sys, time, urllib.request, os
from concurrent.futures import ThreadPoolExecutor
def wait(port):
for _ in range(100):
try: urllib.request.urlopen(f"http://127.0.0.1:{port}/ping", timeout=1).read(); return
except Exception: time.sleep(0.1)
raise SystemExit(f"server on {port} did not start")
def burst(port, n=20):
t = time.perf_counter()
with ThreadPoolExecutor(n) as ex:
res = list(ex.map(lambda i: urllib.request.urlopen(f"http://127.0.0.1:{port}/item/{i}", timeout=30).status, range(n)))
return time.perf_counter() - t, set(res)
servers = [
("WSGI, one request at a time (wsgiref)", [sys.executable, "serve_wsgiref.py", "8101", "single"], 8101),
("WSGI, one thread per request", [sys.executable, "serve_wsgiref.py", "8102", "threaded"], 8102),
("ASGI async handler (uvicorn)", [sys.executable, "-m", "uvicorn", "apps:asgi_async", "--port", "8103", "--log-level", "error"], 8103),
("ASGI handler that blocks (uvicorn)", [sys.executable, "-m", "uvicorn", "apps:asgi_blocking", "--port", "8104", "--log-level", "error"], 8104),
]
for name, cmd, port in servers:
p = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
wait(port); one, _ = burst(port, 1); el, st = burst(port, 20)
print(f"{name:42s} 1 request {one*1e3:5.0f} ms | 20 at once {el:5.2f} s | statuses {sorted(st)}")
finally:
p.terminate(); p.wait()
framework_demo.py: Flask with Pydantic validation, routing errors, and a raw WSGI call:
import json
from flask import Flask, request, jsonify, g
from pydantic import BaseModel, Field, ValidationError, field_validator
app = Flask(__name__)
class NewOrder(BaseModel):
email: str
quantity: int = Field(gt=0, le=100)
@field_validator("email")
@classmethod
def has_at(cls, v):
if "@" not in v: raise ValueError("must contain @")
return v.lower()
@app.post("/orders")
def create_order():
try:
order = NewOrder.model_validate(request.get_json(force=True, silent=True) or {})
except ValidationError as e:
return jsonify(errors=[{"field": ".".join(map(str, x["loc"])), "msg": x["msg"]} for x in e.errors()]), 422
return jsonify(order.model_dump()), 201
@app.get("/orders/<int:order_id>") # the converter rejects non-integers before your code runs
def get_order(order_id): return jsonify(id=order_id)
@app.before_request
def mark(): g.seen = True
c = app.test_client()
print("valid :", c.post("/orders", json={"email": "A@B.com", "quantity": 3}).status_code, c.post("/orders", json={"email": "A@B.com", "quantity": 3}).get_json())
r = c.post("/orders", json={"email": "nope", "quantity": 0}); print("invalid:", r.status_code, r.get_json())
r = c.post("/orders", json={"email": "a@b.c", "quantity": "5"}); print("'5' as str is coerced by default:", r.status_code, r.get_json()["quantity"], type(r.get_json()["quantity"]).__name__)
print("route typing:", c.get("/orders/7").get_json(), c.get("/orders/abc").status_code, "| unknown path:", c.get("/nope").status_code, "| wrong method:", c.put("/orders").status_code)
# what the framework hides: a WSGI call by hand
from io import BytesIO
body = json.dumps({"email": "x@y.z", "quantity": 1}).encode()
environ = {"REQUEST_METHOD": "POST", "PATH_INFO": "/orders", "wsgi.input": BytesIO(body), "CONTENT_LENGTH": str(len(body)), "CONTENT_TYPE": "application/json", "SERVER_NAME": "x", "SERVER_PORT": "80", "wsgi.url_scheme": "http", "wsgi.errors": BytesIO(), "wsgi.version": (1, 0), "wsgi.multithread": False, "wsgi.multiprocess": False, "wsgi.run_once": False, "SERVER_PROTOCOL": "HTTP/1.1"}
seen = {}
def start_response(status, headers): seen["status"] = status
out = b"".join(app.wsgi_app(environ, start_response))
print("raw WSGI call ->", seen["status"], out.decode().strip())
state_app.py and state_demo.py: module-level state under two worker processes:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
import asyncio, os
counter = 0 # module-level state: one copy PER PROCESS
async def hit(request):
global counter
counter += 1
await asyncio.sleep(0.05) # keeps requests in flight so both workers get some
return JSONResponse({"pid_tag": os.getpid() % 1000, "counter": counter})
async def ping(request): return JSONResponse({})
app = Starlette(routes=[Route("/hit", hit), Route("/ping", ping)])
"""Run from this folder: starts state_app.py with 2 uvicorn worker processes and shows that module-level state is per process."""
import subprocess, sys, time, urllib.request, json, collections
p = subprocess.Popen([sys.executable, "-m", "uvicorn", "state_app:app", "--port", "8110", "--workers", "2", "--log-level", "error"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
for _ in range(100):
try: urllib.request.urlopen("http://127.0.0.1:8110/ping").read(); break
except Exception: time.sleep(0.1)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(12) as ex:
res = list(ex.map(lambda _: json.load(urllib.request.urlopen("http://127.0.0.1:8110/hit")), range(12)))
by = collections.defaultdict(list)
for r in res: by[r["pid_tag"]].append(r["counter"])
print("2 workers, 12 requests; counter values seen per worker process:", dict(by))
finally:
p.terminate(); p.wait()
Sources
- Python documentation, Data Structures,
collections,heapqand Time complexity of built-in operations. - pandas documentation, Copy-on-Write, Scaling to large datasets, Categorical data and Working with missing data.
- Python Enhancement Proposals, PEP 3333: Python Web Server Gateway Interface v1.0.1; the ASGI specification.
- Documentation of Flask, Django (deployment, ASGI support), Starlette, FastAPI and uvicorn (workers).
- Pydantic documentation, Models, Validators and Strict mode.
- Gunicorn documentation, Design (worker types and how many to run).
Practise this topic: Python