top of page

Muru Examines Python’s Lazy-Import Tradeoffs

11 minutes ago
10 min read
Illustrative photograph. Photo: Bibek ghosh / Pexels.

Muru’s November 22 developer report challenges a familiar optimization: making Python applications start faster can make their first requests slower. Graham Dumpleton’s latest installment examines where deferred imports move work, who waits for it, and whether workers lose opportunities to share memory.

The project is an AI-assisted Rust server targeting Python applications using ASGI, an asynchronous application interface, and WSGI, its synchronous counterpart. Dumpleton introduced the work earlier in November. The new development is his analysis of import behavior, rather than a fresh server launch or an independently verified performance milestone.

The central choice is loading dependencies before creating worker processes, which handle requests, versus loading them when requests first need them. Gunicorn already exposes a related preloading option. Dumpleton’s argument puts pressure on developers treating shorter startup as sufficient evidence of better application performance, especially when their deployment already protects users from startup delays.

Muru Makes Import Costs the New Focus

The latest report treats import optimization as a workload decision, with consequences that extend beyond application startup.

Dumpleton’s lazy-import analysis follows earlier installments about worker architecture, releases, development tooling, and request tracing. It connects those pieces through a practical question: when should an application pay for loading its dependencies?

An import loads and initializes a Python module, a reusable unit of code. That operation can trigger additional imports, creating a chain of work. A feature requiring a spreadsheet library can therefore load more than the application code directly names.

Lazy imports defer that loading until code first uses the imported name. The attraction is straightforward: dependencies that remain unused need not consume startup time. For applications with optional features, that can avoid work entirely.

A web server adds another consideration. According to Dumpleton, the server imports an application in a host process before creating workers. That ordering allows workers to inherit already loaded modules, changing both when the work happens and how often it occurs.

His example uses Flask, a Python web framework, to build a small shop. A stock report depends on pandas, a data-analysis library. Spreadsheet downloads depend on openpyxl, a library for creating and reading Excel workbooks.

Those features have different import arrangements. The report loads its dependency inside the request-handling function. The spreadsheet feature initially brings its dependency into the application during startup. The example therefore exposes both deferred and eager loading within the same application.

Dumpleton runs the example on Python 3.14 with two workers. He describes tools for inspecting the host’s imports alongside modules loaded later by individual workers. The important comparison spans both stages, rather than stopping when the application becomes available.

That makes the report more specific than a general recommendation to defer heavy libraries. It asks whether dependencies remain unused across actual worker lifetimes. A library that every worker eventually needs creates a different calculation from one that most workers never touch.

The timing also matters. Simon Willison’s November 21 Python server overview highlighted the project and its detailed development account. The November 22 installment adds a concrete optimization question to that broader introduction. Neither publication date establishes that the server has completed production validation.

Faster Startup Does Not Always Reach Users

A shorter startup matters differently when existing workers continue serving traffic and when a visitor waits for an application to wake.

Dumpleton describes a deployment model that starts a replacement release beside the version already serving requests. Traffic moves after the replacement becomes ready. Under that arrangement, application imports consume deployment time without necessarily delaying visitors using the existing release.

Readiness is the check that determines whether an instance can accept traffic. If that check waits for application loading and worker initialization, users do not encounter those startup costs through ordinary routing. Deferring dependencies can move previously hidden work into visible requests.

This distinction puts application developers and deployment owners on the same decision path. Developers can reduce the time their application takes to load. Operators must establish whether that reduction improves rollout speed, user latency, resource use, or some combination.

Containers, isolated application execution environments, create a related situation. A faster start can help additional instances become available sooner. That remains useful even when routing keeps requests away until those instances pass their readiness checks.

However, protecting the initial application load does not automatically protect every optional feature. A worker can be ready to serve its main routes while still lacking a dependency needed by a less common endpoint. The first request reaching that endpoint pays the deferred cost.

Scaling to zero changes the balance again. This means stopping idle serving capacity until traffic returns. Dumpleton describes a case where the host itself has stopped, leaving the next request waiting for application startup.

Here, avoiding an unnecessary import can directly reduce the visitor’s wait. But the benefit depends on which feature that visitor requests. Waking the application quickly offers less help if the same request immediately triggers the deferred dependency.

Development introduces another legitimate priority. Reloading means restarting application execution after a code change. When that happens repeatedly, avoiding unused dependencies can shorten the feedback loop for developers working on unrelated features.

These are separate experiences with separate measures of success. A development reload, a protected production rollout, and a request waking a stopped application should not share an optimization verdict merely because each involves startup.

The forced response is better measurement across deployment stages. Teams evaluating lazy imports need to identify who waits, which dependencies move into request handling, and whether startup improvements survive realistic traffic.

That requirement also limits broad performance claims. A faster host load establishes that one stage became shorter. It does not establish that the application serves users faster, handles more traffic, or consumes less memory throughout its lifetime.

Loading Once Competes With Loading on Demand

The main tradeoff is paying once before workers start versus paying later in each worker that needs a dependency.

The server’s pre-fork design creates workers by copying an initialized process. Copy-on-write memory lets those processes initially share memory pages until changes require private copies. Loading modules beforehand can therefore support sharing across workers.

Deferring an import changes that sequence. Each worker that first needs the module loads it separately. Workers that never need it avoid that work, while workers that do need it incur initialization during their own execution.

This is why the number of workers using a dependency matters more than its import cost alone. An expensive dependency can be a sensible deferral candidate when few workers encounter its feature. The same dependency becomes less attractive when every worker eventually loads it.

The underlying choice predates this project. Gunicorn’s documented application preloading option loads application code before workers fork. Its documentation describes potential memory savings and faster server boot, while also discussing differences in application reloading.

That comparison gives the new report an established reference point. Preloading itself is an existing serving strategy. The distinctive emphasis here is connecting host import costs with evidence about what individual workers subsequently load.

Process architecture also limits comparisons. Uvicorn’s documented worker process model uses spawning rather than pre-forking for its built-in multiprocess manager. Spawning starts separate processes, so identical assumptions about inherited application state do not apply.

Those differences do not identify a universal winner among servers. They establish why import advice needs to name the deployment model. A result obtained with inherited modules cannot automatically describe a server configuration that starts workers independently.

Dumpleton’s shop example makes the distinction tangible. Initially, only one worker needs the spreadsheet dependency. The other avoids loading it. Later, additional downloads reach both workers, changing the evidence behind the earlier recommendation.

The report feature moves in the opposite direction. Both workers load its deferred dependency, duplicating work that the host could have performed before creating them. According to Dumpleton, explicitly preloading that module moves initialization out of those first report requests.

Worker lifetimes further complicate the picture. A long-running worker has more opportunities to encounter an uncommon feature. A worker replaced before that request arrives might avoid the dependency throughout its useful life.

Traffic distribution matters for the same reason. A feature can represent a small share of requests while eventually reaching every long-lived worker. Describing it as “rare” does not establish that deferral saves work across the serving pool.

The economic question is therefore operational rather than stylistic. Teams need to weigh startup delay, first-use latency, and memory sharing against how often dependencies remain unused. A preference for eager or lazy imports cannot replace those workload facts.

First Requests Reveal Where the Work Moved

The report’s strongest evidence is the connection between shorter host loading and import work appearing inside requests.

Dumpleton measures the shop’s host load with import recording disabled, acknowledging that profiling adds work. Profiling is the collection of execution measurements used to locate costs. Separating diagnostic runs from timing comparisons helps avoid confusing measurement overhead with application behavior.

Across five starts for each configuration, he reports median host loading of 83 milliseconds without deferral and 35 milliseconds with it. A median is the middle observation after ordering measurements. These figures describe his example, rather than a general server benchmark.

The deferred work then appears in the spreadsheet request. His example header attributes 52.03 milliseconds to importing modules within an application response taking 56.89 milliseconds. The shorter host load therefore does not mean the dependency became cheaper to initialize.

The server exposes those measurements through Server-Timing, an HTTP response header browsers can display as timing information. Its import entry distinguishes module loading from other request work. That gives developers a way to connect a startup change with its later effect.

Another distinction appears when requests overlap. Python’s import lock coordinates module initialization so concurrent execution does not independently initialize the same module. A request waiting on that lock can experience delay without performing the import itself.

In Dumpleton’s concurrent report example, one request loads the dependency while another waits. The recorded headers distinguish import time from import-wait time. Both requests experience the initialization episode, despite only one doing the loading.

That observation broadens the cost beyond a single unlucky first visitor. Requests sharing a worker can encounter related delays when they need a module whose initialization remains unfinished. Counting only the request that imported it understates the experience.

According to the report, preloading the report module changes those first responses. Dumpleton records first report requests between 4.5 and 9 milliseconds after moving the dependency into the host’s load. Those are author-reported results from the sample application.

The example supports a narrow conclusion: changing import placement changed where this application spent time. It does not establish a production throughput advantage, a general memory reduction, or superiority over competing servers.

The tooling context helps explain why Dumpleton emphasizes request evidence. His earlier request tracing installment connects application operations with development diagnostics. That example uses a shop to expose repeated database queries and outbound calls.

Those are constructed examples rather than customer deployment results. Their value is illustrating what developers can inspect. The import report extends that approach by connecting host loading, first-use costs, and the workers that ultimately needed each module.

Python Compatibility Keeps the Verdict Conditional

Language support makes deferral easier to express, but compatibility and actual usage still determine whether it helps.

Python’s explicit lazy imports specification targets Python 3.15 and defines loading modules when imported names are first used. It also provides a transitional module declaration that earlier interpreters ignore. New syntax and backward-compatible declarations therefore require different handling.

Dumpleton describes using unwrapt, his import-deferral package, to support selected deferral behavior on earlier Python versions. That distinction matters for teams supporting multiple interpreters. Writing a declaration does not by itself guarantee the same execution behavior everywhere.

His report identifies a specific limitation in the older-version approach: unwrapt can defer a plain import, while native support can also defer a from-import. A from-import binds selected names from a module. Some application code therefore needs rewriting before the older-version mechanism can defer it.

Even successful deferral can save nothing when initialization immediately uses the dependency. The shop’s spreadsheet library provides an example: a nested dependency is used while its importing module is still loading. Delaying that import merely postpones it within the same initialization sequence.

This is a useful limit on automated suggestions. An import can look expensive in a report without representing avoidable work. Tools need to distinguish dependencies that remain untouched from dependencies immediately required to construct the importing module’s state.

The same caution applies to traffic-derived advice. A worker report covers requests already observed. If an optional feature has not been exercised, the absence of an import establishes only that the sampled traffic did not need it.

A broader sample can reverse the recommendation. Dumpleton shows the spreadsheet dependency moving from one worker to both after further downloads. That change illustrates why developers should revisit conclusions after representative traffic rather than treating an early recommendation as permanent.

There is also a verification gap. The examples come from the project’s author and have not been independently reproduced here. Detailed output improves inspectability, but it cannot substitute for outside testing across different applications, dependencies, and deployment environments.

AI assistance adds another reason to examine evidence carefully. Willison describes the project as AI-assisted, while Dumpleton emphasizes his responsibility for deciding what remains inadequate. Neither the implementation language nor the development method establishes operational reliability.

That does not require dismissing the work. It means separating documented behavior from broader confidence. Import visibility can be useful while questions remain about compatibility, failure handling, maintenance, and performance outside the example.

For engineering teams, those distinctions belong in a shared record of experiments and decisions. A searchable knowledge base can preserve workload assumptions alongside results. Without those assumptions, a successful optimization can become misleading guidance when copied into another service.

What Would Strengthen the Case Next

The next useful evidence is reproducible behavior across workloads, interpreter versions, and changing worker populations.

The first signal is independent reproduction of the import-placement results. Tests should follow dependencies from host initialization through worker creation and first requests. They should also compare subsequent requests, so a brief startup gain does not obscure repeated worker costs.

A convincing reproduction would disclose application dependencies, interpreter versions, worker counts, and traffic composition. Memory measurements would help test the sharing argument alongside timing. Evidence that the same relationships persist would strengthen the report’s central tradeoff analysis.

Results that vary substantially would still be informative. They would narrow where the measurements apply and identify which dependencies or deployment choices dominate. The goal is understanding the boundary of the behavior, rather than obtaining an impressive isolated number.

The second signal is compatibility evidence spanning native and earlier-version deferral. Teams need to know whether selected imports defer as intended and whether startup-time dependency use cancels expected savings. They also need clear diagnostics when an import form receives different treatment.

Successful compatibility testing would strengthen confidence in applying the same policy across supported environments. Repeated surprises would weaken the case for broad adoption and favor more selective use. Documentation should make those differences visible before teams change application loading.

The third signal is observation across realistic worker lifetimes. A useful deployment record would track which workers load each optional dependency as traffic arrives and capacity changes. That would test whether apparently rare features remain rare at the worker level.

If most workers avoid a dependency before replacement, deferral has a stronger case for avoiding unnecessary initialization. If nearly every worker loads it, preloading deserves renewed consideration. Either result would turn an assumption about traffic into evidence about the serving pool.

These signals also keep evaluation focused. A growing feature list would not resolve the import question by itself. What matters is whether the diagnostics help teams choose a policy that improves their actual combination of startup, request latency, and resource use.

For readers considering Muru, start with an application whose optional dependencies and deployment behavior you already understand. Preserve the baseline, identify who waits during startup, and compare first-use requests with later traffic. Keep measurements tied to specific worker lifetimes and interpreter versions. Ask whether an avoided import remains avoided, or simply reappears in every worker after enough requests. Treat the published shop as a testable example, then examine whether your own application follows the same pattern. Read the underlying reports before changing production settings, and retain the conditions behind each result. The decision worth making is when your application should pay for its dependencies, supported by evidence about the people and processes that otherwise pay later.

Get your work moving in remio

See what needs your attention and take the next step in Workspace.

remio currently supports Windows 10+ (x64) and Macs with Apple silicon.

Your AI Partner at Work
Get more done with remio

Plan. Create. Deliver.
All in one place.

bottom of page