Skip to content

perf(serverless): defer heavy imports until first use - #772

Draft
deanq wants to merge 2 commits into
mainfrom
deanq/SLS-1129-lazy-imports-worker
Draft

deanq wants to merge 2 commits into
mainfrom
deanq/SLS-1129-lazy-imports-worker

Conversation

@deanq

@deanq deanq commented Nov 29, 2026

Copy link
Copy Markdown
Member

Summary

import runpod loaded the whole serverless worker stack (aiohttp, fastapi, the job loop) even for callers that only use the API client or the endpoint client. This defers those imports until they are used.

Changes

  • runpod/__init__.py, runpod/serverless/__init__.py and runpod/serverless/utils/__init__.py expose the same names through module __getattr__ (PEP 562) instead of eager imports.
  • runpod.serverless.start() imports the worker when it is called.
  • runpod.version is resolved lazily.
  • runpod.api_key is read from the environment on first access instead of at import.

Impact

Python 3.13, median wall time including interpreter start:

Before After
import runpod 33 ms, 158 modules 17 ms, 76 modules
import runpod.serverless 125 ms, 538 modules 35 ms, 178 modules

Behavior change

RUNPOD_API_KEY set after import runpod but before the first read of runpod.api_key is now picked up. Assigning runpod.api_key explicitly works as before.

Tests

New tests/test_lazy_imports_worker.py (18 tests) checks which modules each import loads and that every public name still resolves. 9 failed before the change. Full suite passes.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 22:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved lazy-export regressions break API discovery and existing module access, while concurrent API-key loading can overwrite explicitly assigned credentials.

3 open issues
What changed in this PR

This PR reduces import overhead for API and endpoint clients by deferring serverless worker dependencies and credential lookup until first use.

Changes:

  • Adds lazy resolution for package exports and imports the worker when start() runs.
  • Defers version metadata imports and API-key loading, preserving explicit key assignment and reload behavior.
  • Adds isolated-process tests for import weight, export availability, and credential resolution.
File Description
tests/​test_lazy_imports_worker.py Adds import-weight and compatibility regression tests.
runpod/​version.py Loads metadata helpers only when needed.
runpod/​serverless/​utils/​__init__.py Resolves utility exports lazily.
runpod/​serverless/​__init__.py Defers worker and selected helper imports.
runpod/​__init__.py Adds lazy CLI access and first-read API-key resolution.

Review effort: Balanced

Comment thread runpod/__init__.py Resolved
Comment thread runpod/serverless/__init__.py Resolved
Comment thread runpod/serverless/utils/__init__.py Resolved
@deanq
deanq force-pushed the deanq/SLS-1129-lazy-imports-worker branch 3 times, most recently from 736295e to 6660c56 Compare December 4, 2026 00:18
deanq added 2 commits December 4, 2026 12:18
`import runpod.serverless` loaded aiohttp, the http client, the upload
and download helpers and the credential parsers even for processes that
only register hooks or read the version. Resolve those on first access
instead (PEP 562), keeping every existing name importable:

- serverless: worker, progress_update, VolumeCache and utils load lazily
- serverless.utils: download/upload/VolumeCache load lazily
- runpod.api_key is read from the credential store on first access, not
  at import (assignment and reload behave as before)
- runpod.version: importlib.metadata is only touched when the generated
  _version module is absent
- runpod.cli resolves lazily as an attribute

Adds subprocess guard tests for the absence of heavy modules and for the
public names staying resolvable.
- runpod.api_key: first read uses setdefault so a key assigned by
  another thread while the credential store is read is not overwritten
- runpod.serverless and runpod.serverless.utils: add __dir__ listing the
  lazy exports without importing them
- runpod.serverless.utils: resolve the rp_download, rp_upload and
  rp_volume_cache submodule attributes lazily, as the former eager
  imports exposed them; __all__ is unchanged
@deanq
deanq force-pushed the deanq/SLS-1129-lazy-imports-worker branch from 6660c56 to e17a894 Compare December 4, 2026 20:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants