Wrap-up and resources
Key Takeaways
- 1. RESTful APIs: resources with clear names, the method carries the verb (GET reads, POST creates, PUT/DELETE are idempotent), and the status code tells the truth (201 created, 204 no body, 401 identity, 403 permission). Consume with curl and requests (with a
timeout), build with Flask, secure with HTTPS, tokens and password hashes, and document the contract with OpenAPI. - 2. Asynchronous Python: most slowness is waiting, not computing;
async defdefines a coroutine andawaithands the turn back to the event loop. Writingasyncalone gains nothing, the gain comes fromgather(results in argument order) oras_completed(in completion order), whilecreate_taskstarts work immediately. And any blocking call inside a coroutine freezes everything. - 3. Async generators & comprehensions: a
yieldinside anasyncfunction makes it an async generator rather than a coroutine, so you consume it withasync foror the comprehension[v async for v in src], and annotate itAsyncGenerator[Y, None]with two parameters only. The comprehension is a syntax shorthand, not parallelism: the waits inside one source are sequential, and parallelism comes fromgatheracross independent sources.
Glossary
| Term | Definition | Part |
|---|---|---|
| REST | An architectural style for services over HTTP that treats things as addressable resources and follows six constraints | 1 |
| Resource | Anything with a URL that can be represented (a drink, an order, a user) | 1 |
| Safe / Idempotent | Safe does not change server state; idempotent means repeating the request leaves the same end state | 1 |
| Status code | A three-digit number summarizing the outcome (2xx success, 4xx client error, 5xx server error) | 1 |
| Endpoint | A path plus an HTTP method together (like GET /drinks/3) | 1 |
| Header | A name/value pair describing the request or response (Content-Type, Authorization) | 1 |
| curl | A terminal tool for sending HTTP requests and inspecting responses | 1 |
| Authentication / Authorization | The first asks who you are; the second asks what you are allowed to do (401 vs 403) | 1 |
| Bearer token | A token sent in the Authorization header that grants access to whoever holds it, so never leak it | 1 |
| JWT | A three-part token (header.payload.signature) that is signed but not encrypted | 1 |
| OpenAPI | A format for describing an API (paths, parameters, responses) in a YAML or JSON file | 1 |
| Coroutine | A function defined with async def that can pause at an await and resume at the same line with all its variables | 2 |
| Event loop | The loop that hands turns to ready tasks and sleeps when none is ready; opened with asyncio.run | 2 |
await | Runs an awaitable and hands the turn back to the loop while it waits; forbidden outside an async function | 2 |
| Task | A coroutine scheduled on the loop, so it starts immediately; it can be cancelled with cancel() and inspected with done() | 2 |
gather / as_completed | The first runs them all and returns results in argument order; the second hands you each one the moment it finishes | 2 |
| I/O-bound / CPU-bound | Slowness caused by waiting versus slowness caused by computing; the first is cured by asyncio, the second by multiprocessing | 2 |
| Concurrency vs parallelism | Concurrency is switching between jobs on one thread; parallelism is real execution at the same instant on several cores | 2 |
| CancelledError | The exception delivered inside a task when it is cancelled, which is why finally is the right place to clean up | 2 |
| Async generator | An async def function containing yield; its type is async_generator, it is not a coroutine, and await is not valid in front of it | 3 |
async for | A loop that asks for the next item with __anext__ and hands the turn back to the loop between every two items | 3 |
| Async comprehension | [v async for v in src]: a syntax shorthand for an async for loop, working in list, set and dict forms | 3 |
| StopAsyncIteration | The exception announcing the end of async iteration, caught by the async for loop on your behalf | 3 |
AsyncGenerator[Y, S] | The async generator type annotation, with two parameters only; no return parameter because it returns no final value | 3 |
aclose() | Closes an async generator that was not consumed to the end, so the cleanup code in finally is reached | 3 |
| SSR | Building HTML on the server and sending it ready. | 4 |
| Template | HTML with places filled in by data. | 4 |
| Auto-escaping | Turning special characters into safe text before display. | 4 |
| WebSocket | One open connection where both sides send whenever they like. | 5 |
| Upgrade | An HTTP header asking to switch the connection to WebSocket. | 5 |
| Broadcast | Sending a message to everyone connected. | 5 |
Sources
The best outside resources for these lessons, classified by kind and level. All are open to everyone and need no account on any platform. Use the filters to narrow the list.
0 resources
Video (2)
VideoTech With Tim, Asyncio in Python: Full TutorialA gradual video lesson covering the event loop, coroutines, tasks and synchronization; a good first visual pass over the idea.youtube.comBeginnerVideoArjanCodes, How To Easily Do Asynchronous Programming With AsyncioExplains async/await and then compares it with threads and processes, which is the comparison that settles when to use each tool.youtube.comIntermediate
Official documentation (11)
Official docsMDN, HTTP request methodsGET, POST, PUT, DELETE and their safety and idempotency properties.developer.mozilla.orgAdvancedOfficial docsMDN, HTTP response status codesReference for status codes across the five classes.developer.mozilla.orgAdvancedOfficial docsasyncio, Asynchronous I/OThe library's home page: what asyncio is, when it fits, and a map of its high-level APIs.docs.python.orgIntermediateOfficial docsCoroutines and TasksThe precise reference for gather, create_task, as_completed and the Task object with all its methods.docs.python.orgIntermediateOfficial docsA conceptual overview of asyncioAn official HOWTO that builds the mental model: what await does under the hood, and why awaiting a task differs from awaiting a coroutine.docs.python.orgAdvancedOfficial docsPEP 492, Coroutines with async and await syntaxThe document that introduced async and await in Python 3.5, and the reasoning behind each syntax decision.peps.python.orgAdvancedOfficial docsrandom.uniformReturns a float between a and b, with the important caveat that whether b itself is included depends on floating-point rounding.docs.python.orgBeginnerOfficial docsPEP 525, Asynchronous GeneratorsThe document that allowed yield and await in the same body, starting with Python 3.6.peps.python.orgAdvancedOfficial docsPEP 530, Asynchronous ComprehensionsThe original reference for the [v async for v in src] form, and where await is valid inside comprehensions.peps.python.orgAdvancedOfficial docsWhat's New In Python 3.6, PEP 530A short official summary of both features with brief examples, quicker than reading the full PEPs.docs.python.orgIntermediateOfficial docstyping, generator annotationsWhere AsyncGenerator comes from, what its two parameters mean, and why there is no third.docs.python.orgIntermediate
Articles and explainers (9)
ArticleFlask, QuickstartRoutes, methods and JSON responses.flask.palletsprojects.comAdvancedArticleRequests, QuickstartSending requests and reading status and body from Python.requests.readthedocs.ioAdvancedArticlecurl, Scripting HTTP RequestsHow to try your API from the terminal with -X, -H and -d.curl.seAdvancedArticleOWASP, REST Security Cheat SheetA checklist of security practices for REST APIs.cheatsheetseries.owasp.orgAdvancedArticleOpenAPI, IntroductionDescribing an API in a machine-readable file.learn.openapis.orgAdvancedArticleRFC 7519, JSON Web TokenThe official reference for the JWT structure.datatracker.ietf.orgAdvancedArticleFielding, REST (Chapter 5)The original definition of the REST style and its constraints.ics.uci.eduAdvancedArticleReal Python, Python's asyncio: A Hands-On WalkthroughThe most thorough practical walkthrough available: from a first coroutine to async generators and comprehensions, with complete examples.realpython.comIntermediateArticleReal Python, Asynchronous Iterators and Iterables in PythonThe closest match to this part: async generators, __aiter__/__anext__, and comprehensions in all four forms.realpython.comIntermediate
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Official docs2
Done with this lesson?
Sign in to save your progress.