Skip to content

perf: run sync modules in bounded thread pool executor#42

Merged
isaacbmiller merged 1 commit intomainfrom
isaac/perf-bounded-executor
Feb 28, 2026
Merged

perf: run sync modules in bounded thread pool executor#42
isaacbmiller merged 1 commit intomainfrom
isaac/perf-bounded-executor

Conversation

@isaacbmiller
Copy link
Copy Markdown
Contributor

@isaacbmiller isaacbmiller commented Feb 28, 2026

Split from #38 (was 3/5, now standalone)

Changes

Sync forward() and batch() calls previously ran directly on the async event loop, blocking all other requests. This PR moves them into a dedicated ThreadPoolExecutor with proper contextvars propagation so dspy.context(lm=...) carries into worker threads.

Keeps the existing hasattr(instance, 'aforward') check for async detection -- no changes to discovery.

Configuration

  • --sync-workers N CLI flag
  • server.sync_worker_threads in dspy.config.yaml
  • Default: min(32, cpu_count + 4) (matches Python's default)

Files changed (7)

  • src/dspy_cli/server/executor.py (new, 51 lines)
  • src/dspy_cli/server/execution.py -- use run_sync_in_executor
  • src/dspy_cli/server/app.py -- init/shutdown executor
  • src/dspy_cli/commands/serve.py -- --sync-workers option
  • src/dspy_cli/server/runner.py -- passthrough
  • tests/test_executor.py (new, 4 context propagation tests)
  • tests/test_commands_smoke.py -- updated expected kwargs

Testing

127 tests pass (123 existing + 4 new).

Move sync forward() and batch() calls off the event loop into a
dedicated ThreadPoolExecutor with context variable propagation.
Keeps the existing hasattr(instance, 'aforward') check for async
detection. Configurable via --sync-workers flag or
server.sync_worker_threads in dspy.config.yaml.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@isaacbmiller isaacbmiller force-pushed the isaac/perf-bounded-executor branch from 340e444 to 0aa12e7 Compare February 28, 2026 21:08
@isaacbmiller isaacbmiller changed the base branch from isaac/perf-async-detection to main February 28, 2026 21:09
@isaacbmiller isaacbmiller merged commit 2ddd4c9 into main Feb 28, 2026
1 check passed
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