Skip to content

Experiment: defer heavy imports in logging handlers#20966

Draft
zzstoatzz wants to merge 1 commit intomainfrom
codex/cli-startup-logging-handlers
Draft

Experiment: defer heavy imports in logging handlers#20966
zzstoatzz wants to merge 1 commit intomainfrom
codex/cli-startup-logging-handlers

Conversation

@zzstoatzz
Copy link
Collaborator

Summary

This PR is a focused experiment to reduce common CLI startup overhead by deferring heavy imports in prefect.logging.handlers until they are actually needed.

It intentionally does not change plugin initialization semantics or prefect.context behavior.

What changed

  • Replaced eager module-level imports in src/prefect/logging/handlers.py with cached local loader helpers for:
    • prefect.context
    • prefect.client.orchestration.get_client
    • prefect.client.schemas.actions.LogCreate
    • logging settings constants from prefect.settings
    • from_timestamp and MissingContextError
  • Updated handler codepaths to use those helpers lazily.

Why

This is the narrowest low-risk candidate from recent CLI startup investigations. If it helps, we can keep it; if it does not, this still gives us concrete data for future import-graph work.

Local validation

  • uv run pytest tests/test_logging.py -q
  • uv run pytest tests/cli/test_version.py tests/cli/test_config.py -q
  • uv run ruff check src/prefect/logging/handlers.py

Local bench note

Local cli-bench results were noisy and not consistently positive on my machine, so this is being sent to CI as an empirical check rather than a claimed win.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 4, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing codex/cli-startup-logging-handlers (a99da08) with main (f8874f4)

Open in CodSpeed

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.

1 participant