liveBook · Manning

Model worker

As explained in Deep Learning with PyTorch, Second Edition

A background execution component that pulls requests from a queue and runs model inference on them.

A model worker is a background execution component that pulls requests from a queue and runs model inference. It lets an API keep accepting requests while inference runs separately. section 17.1.4

Figure 17.2 Dataflow with request batching and streaming
figure

Follow the flow from client requests into a queue, through batched model execution, and back to clients as streamed tokens. section 17.1.4

A typical worker coordinates several pieces:

  • Collect requests until a batch is full or the oldest request reaches a maximum wait time. section 17.1.4
  • Run on a background thread while the API handles incoming requests concurrently. section 17.1.4

FastAPI worker setup

Start model_worker in the application's lifespan, enqueue each request with a unique identifier, and return a StreamingResponse for its results.

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains queue timing, worker lifecycle, token delivery, and synchronization, but leaves batching and streaming helper implementations abbreviated.
Examples
Uses a FastAPI server, thread-safe queues, token generation, curl requests, and concurrent shell requests as concrete artifacts.
What it assumes
Assumes familiarity with FastAPI endpoints and model inference while introducing queue coordination, worker lifecycle, and streaming behavior.
What you can do after
Build it from scratch as a FastAPI background worker that batches inference and streams generated tokens per request.
How it connects
Connects model workers with request queues, request batching, background threads, asynchronous generators, and streaming responses.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.