Skip to content

perf: run async handlers concurrently - #101

Merged
j03-dev merged 1 commit into
perf/t1-release-allocatorfrom
perf/t3-async-concurrency
Oct 31, 2026
Merged

j03-dev merged 1 commit into
perf/t1-release-allocatorfrom
perf/t3-async-concurrency

Conversation

@j03-dev

@j03-dev j03-dev commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

T3: run async handlers concurrently

Stacked on #100 (perf/t2-batch-handoff); base this PR on perf/t2-batch-handoff until the stack lands.

Problem

In async mode, process_requests awaited every handler on the single consumer task, so async def handlers ran one at a time. 20 concurrent requests to a handler that does await asyncio.sleep(0.5) took ~10s.

Change

  • Capture the Python task locals once with pyo3_async_runtimes::tokio::get_current_locals.
  • When the server is in async mode, spawn each request onto Tokio with pyo3_async_runtimes::tokio::scope(locals, ...) so the handler still sees the correct Python event loop and contextvars.
  • Track spawned requests in a JoinSet; abort and drain it on shutdown.
  • Sync mode is unchanged: requests are still processed sequentially on the existing consumer.

Before/after

tests/test_http_server.py::test_async_handlers_run_concurrently: 20 concurrent GET /slow, each handler await asyncio.sleep(0.5).

build elapsed
T2 (before) 10.03 s
T3 (after) 0.99 s

Test assertion: < 2.0 s. Before fails, after passes.

The test runs the async app in a subprocess (tests/async_app.py) so it does not collide with the session-scoped sync server: ctrlc::set_handler is process-global and only one server can own it per process.

Sync-mode regression check (swapped .so, 10 interleaved rounds x 10s, server 0-3, wrk 4-7):

metric T2 T3 delta
median req/s 62,784 61,998 -1.25%
paired median req/s (T3/T2) -0.7%
CPU ms/request 0.03128 0.03148 +0.6%
context switches/request 0.18457 0.18284 -0.9%

Sync mode is within noise; the change only affects async mode.

Validation

  • cargo fmt --check: pass
  • cargo clippy --all-targets --all-features: only the pre-existing too_many_arguments warning in src/response.rs
  • cargo test --lib --bins: pass
  • pytest -q tests: 39 passed

CI note

Linux wheels are now built on manylinux_2_28 (glibc >= 2.28) so mimalloc 0.1.52 builds on the cross targets; the older manylinux2014 cross toolchains lack stdatomic.h and reject -Wdate-time. The deltas above were measured on x86_64 with the same allocator on both sides. On the rebased stack, cargo fmt --check, cargo test --lib --bins, and the full pytest suite (39 passed) all pass.

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9395fc28-76a7-403b-9136-5bb2b6ca9cbd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@j03-dev
j03-dev force-pushed the perf/t2-batch-handoff branch from f091976 to 3197ba8 Compare October 31, 2026 04:30
@j03-dev
j03-dev force-pushed the perf/t3-async-concurrency branch from 3617a6b to 5d2b7be Compare October 31, 2026 04:30
@j03-dev
j03-dev force-pushed the perf/t2-batch-handoff branch from 3197ba8 to e00e8e8 Compare October 31, 2026 04:34
@j03-dev
j03-dev force-pushed the perf/t3-async-concurrency branch from 5d2b7be to 3fe2f37 Compare October 31, 2026 04:34
@j03-dev
j03-dev force-pushed the perf/t2-batch-handoff branch from e00e8e8 to b3e2330 Compare October 31, 2026 05:06
@j03-dev
j03-dev force-pushed the perf/t3-async-concurrency branch from 3fe2f37 to 96f6f5c Compare October 31, 2026 05:06
@j03-dev
j03-dev force-pushed the perf/t2-batch-handoff branch from b3e2330 to 1a1a7dd Compare October 31, 2026 05:06
@j03-dev
j03-dev force-pushed the perf/t3-async-concurrency branch from 96f6f5c to 00be212 Compare October 31, 2026 05:06
Base automatically changed from perf/t2-batch-handoff to perf/t1-release-allocator October 31, 2026 06:08
@j03-dev
j03-dev force-pushed the perf/t3-async-concurrency branch from 00be212 to 5152df6 Compare October 31, 2026 06:11
@j03-dev
j03-dev merged commit 59a3ad0 into perf/t1-release-allocator Oct 31, 2026
17 checks passed
j03-dev added a commit that referenced this pull request Nov 4, 2026
* perf: use mimalloc and enable fat LTO

* ci: build manylinux wheels on glibc 2.28

* perf: batch request handoff with recv_many (#100)

* perf: run async handlers concurrently (#101)

* chore: bump pyo3 version

* chore: format code

* fix: ci
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