Build it: the port and five at once

Loading your place in this lesson…

Build it: the port and five at once

Port chapter 1's speed script to Python with tests, then send five requests at once with asyncio. Part of chapter 3: Python for JavaScript developers (optional bridge).

The port, with tests

Before you start

You need Ollama 0.35.0 with qwen3:0.6b, Node 22.18 or newer, and lesson 1's Python 3.13 venv. On Windows, work in WSL 2.

On Windows: reach Ollama from WSL

Your scripts in WSL must reach Ollama at 127.0.0.1. Pick one way, then run the check.

Your cheat sheet

Every JavaScript and Python pair from the lessons, in one table with its tag. Keep it open while you write.

Get the files

Download these into one new folder inside lab. speed_port.py has five gaps, each marked ___ with a one-line hint.

Read it first

Open speed_port.py: lines_from_chunks cuts bytes into lines, stream_chat yields (seconds, message) pairs, measure turns pairs into numbers. Line 36's := assigns and tests at once.

Fill the gaps, then test

Fill each gap, then run the tests. They replay saved logs and stream from the simulator, so they need no Ollama.

Compare with ours

When every test passes, open our speed_port_solution.py and compare it with yours.

Run it on Ollama

Run your filled port against Ollama, in this folder.

Compare with chapter 1

Then run chapter 1's speed.py or node speed.mjs. Same lines, close numbers. Only the port's last label differs.

Five at once, with asyncio

What you build

Five requests that leave together through asyncio, with the loop kept free, and a report of the wait inside your own code.

Get the files

Download these into one new folder inside lab: the file with the gap, its test, chapter 1's fake server and chapter 1's timing.py.

Chapter 1's chat()

Call chat(model, prompt, think=False, options=...). It blocks until the reply ends, then returns a dict with first_answer_s, end_s and final.

Fill one(i)

In order: wait at the semaphore, stamp sent_s, run chat() without blocking the loop (lesson 4, part 3), then keep the result.

Test it

Run the test. It starts chapter 1's fake server on a free port, then runs your file with a limit of 5, then 1.

Compare with ours

When the test passes, open our five_async_solution.py and compare it with yours.

Run it on Ollama

Ollama keeps its default of one slot. Run with a limit of 5.

Then a limit of 1

Run it again with a limit of 1.