perf: run sync modules in bounded thread pool executor#42
Merged
isaacbmiller merged 1 commit intomainfrom Feb 28, 2026
Merged
Conversation
c078488 to
4165260
Compare
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>
340e444 to
0aa12e7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Split from #38 (was 3/5, now standalone)
Changes
Sync
forward()andbatch()calls previously ran directly on the async event loop, blocking all other requests. This PR moves them into a dedicatedThreadPoolExecutorwith propercontextvarspropagation sodspy.context(lm=...)carries into worker threads.Keeps the existing
hasattr(instance, 'aforward')check for async detection -- no changes to discovery.Configuration
--sync-workers NCLI flagserver.sync_worker_threadsin dspy.config.yamlmin(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-- userun_sync_in_executorsrc/dspy_cli/server/app.py-- init/shutdown executorsrc/dspy_cli/commands/serve.py----sync-workersoptionsrc/dspy_cli/server/runner.py-- passthroughtests/test_executor.py(new, 4 context propagation tests)tests/test_commands_smoke.py-- updated expected kwargsTesting
127 tests pass (123 existing + 4 new).