Skip to content

Improve executor pool configuration and fix idle GIL contention#16

Merged
benoitc merged 1 commit intomainfrom
feature/executor-improvements
Mar 8, 2026
Merged

Improve executor pool configuration and fix idle GIL contention#16
benoitc merged 1 commit intomainfrom
feature/executor-improvements

Conversation

@benoitc
Copy link
Copy Markdown
Owner

@benoitc benoitc commented Mar 8, 2026

Summary

  • Add MIN_EXECUTORS (2) and increase MAX_EXECUTORS to 32 for more flexibility
  • Calculate default executors dynamically as (dirty_cpu_schedulers div 2) + 1
  • Fix GIL contention by acquiring GIL only when processing actual work, not while idle

Root Cause

Idle executor threads were continuously cycling through GIL acquire/release, competing with dirty schedulers running actual Python work via the context-based API (py:call()).

Fix

Changed multi_executor_thread_main() to:

  1. Not acquire GIL at thread start
  2. Wait for work using only pthread mutex/cond (no GIL)
  3. Acquire GIL only when there's actual work to process
  4. Release GIL before signaling completion

- Add MIN_EXECUTORS (2) and increase MAX_EXECUTORS to 32
- Set default executors to 4 (benchmarked sweet spot)
- Fix GIL contention: acquire GIL only when processing actual work,
  not while idle waiting for requests. This prevents idle executor
  threads from competing with dirty schedulers running Python work
  via the context-based API.

Benchmark results (14 dirty CPU schedulers):
- 4 executors: best sync (400k/sec) and concurrent performance
- Context API: stable regardless of executor count (GIL fix verified)
@benoitc benoitc force-pushed the feature/executor-improvements branch from 2841013 to 6d72b2e Compare March 8, 2026 09:50
@benoitc benoitc merged commit 6b63ffe into main Mar 8, 2026
11 checks passed
@benoitc benoitc deleted the feature/executor-improvements branch March 8, 2026 09:53
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