Skip to main content

An event loop for asyncio written in Rust

Project description

rsloop logo

rsloop: An event loop for asyncio written in Rust

rsloop is a PyO3-based asyncio event loop. Each rsloop.Loop still owns a dedicated Rust runtime thread for timers, readiness polling, signal watchers, and transport coordination, but Python callbacks, tasks, and coroutines are driven on the thread that calls run_forever() / run_until_complete() (typically the main thread). The package exposes a Python module at rsloop._loop plus a small Python wrapper in python/rsloop/__init__.py.

The project metadata currently targets Python >=3.8.

Current Surface Area

Today’s codebase provides:

  • rsloop.Loop, rsloop.new_event_loop(), and rsloop.run(...)
  • loop lifecycle:
    • run_forever
    • run_until_complete
    • stop
    • close
    • time
    • is_running
    • is_closed
    • debug flag getters/setters
  • callback scheduling:
    • call_soon
    • call_soon_threadsafe
    • call_later
    • call_at
    • returned Handle and TimerHandle objects
  • future/task helpers:
    • create_future
    • create_task
    • set_task_factory / get_task_factory
    • exception handler storage and dispatch
    • set_default_executor
    • run_in_executor
    • shutdown_asyncgens
    • shutdown_default_executor
  • callback execution inside captured contextvars.Context
  • asyncio.get_running_loop() support for coroutines driven by rsloop
  • asyncio.run(..., loop_factory=rsloop.new_event_loop) support on Python 3.12+
  • Unix fd readiness callbacks via add_reader / remove_reader / add_writer / remove_writer
  • raw socket awaitables:
    • sock_recv
    • sock_recv_into
    • sock_sendall
    • sock_accept
    • sock_connect
  • DNS awaitables:
    • getaddrinfo
    • getnameinfo
  • stream transports and servers:
    • create_server
    • create_connection
    • create_unix_server
    • create_unix_connection
    • connect_accepted_socket
    • returned Server and StreamTransport objects
    • transport.close() / transport.abort() fully close socket transports
  • pipe transports:
    • connect_read_pipe
    • connect_write_pipe
  • subprocess transports:
    • subprocess_exec
    • subprocess_shell
    • returned ProcessTransport and ProcessPipeTransport objects
    • higher-level asyncio.create_subprocess_exec()
    • higher-level asyncio.create_subprocess_shell()
    • low-level support for cwd, env, executable, pass_fds, start_new_session, process_group, user, group, extra_groups, umask, and restore_signals
  • Unix signal handlers via add_signal_handler / remove_signal_handler
  • a free-threaded module declaration via #[pymodule(gil_used = false)]
  • profiler entry points:
    • profile
    • profiler_running
    • start_profiler
    • stop_profiler

Fast Streams

Importing rsloop patches asyncio.open_connection() and asyncio.start_server() by default. That import-time patch is controlled by RSLOOP_USE_FAST_STREAMS and can be disabled with:

export RSLOOP_USE_FAST_STREAMS=0

The patched helpers only take the native rsloop fast-stream path when:

  • the currently running loop is an rsloop.Loop
  • ssl is unset or None

Otherwise they fall back to the stdlib asyncio stream helpers. The fast stream implementation lives in Rust in src/fast_streams.rs, while the lower level transport implementation lives in src/stream_transport.rs.

Current Gaps

  • TLS is not implemented:
    • start_tls() is stubbed
    • ssl=... is rejected for create_server, create_connection, create_unix_server, create_unix_connection, and connect_accepted_socket
  • stream transport flow control is still partial:
    • pause_reading() / resume_reading() work
    • get_write_buffer_size() returns 0
    • get_write_buffer_limits() returns (0, 0)
    • set_write_buffer_limits() is a no-op
  • subprocess support is intentionally incomplete:
    • preexec_fn is unsupported
    • text mode is rejected for higher-level asyncio.create_subprocess_exec() / asyncio.create_subprocess_shell() because the stdlib stream protocol is byte-oriented
    • low-level loop.subprocess_exec() / loop.subprocess_shell() do support text decoding when used with a custom subprocess protocol
  • Unix-only APIs remain Unix-only:
    • create_unix_server
    • create_unix_connection
    • add_signal_handler / remove_signal_handler

Build

Quick check:

cargo check

Release build and editable install:

cargo build --release
uv run --with maturin maturin develop --release

Build release wheels for CPython 3.8 through 3.14, plus the free-threaded 3.14 build, into dist/wheels:

scripts/build-wheels.sh

Publishing

PyPI releases are published from Git tags that start with v, for example v0.1.0. The GitHub Actions workflow builds wheels by calling scripts/build-wheels.sh, builds an sdist, and then publishes the combined artifacts to PyPI with GitHub trusted publishing.

Before the first release, configure the rsloop project on PyPI as a trusted publisher for this repository/workflow.

Use a release build for benchmarks and runtime comparisons. A debug extension will make the Rust loop look much slower than it really is.

Profiling

Profiling support is behind the Cargo feature profiler and is disabled by default. Build/install a release extension with that feature enabled first:

cargo build --release --features profiler
uv run --with maturin maturin develop --release --features profiler

Then wrap the workload you want to inspect:

import rsloop

with rsloop.profile("rsloop-flamegraph.svg", frequency=999):
    rsloop.run(main())

You can also manage the session manually:

import rsloop

rsloop.start_profiler(frequency=999)
try:
    rsloop.run(main())
finally:
    rsloop.stop_profiler("rsloop-flamegraph.svg")

This writes an SVG flamegraph that you can open directly in a browser. Run the profile against a release build or the stack samples will be dominated by debug overhead. If the extension was built without --features profiler, start_profiler() and profile() will raise a runtime error.

Usage

Simple entry point:

import rsloop

async def main():
    ...

rsloop.run(main())

Manual loop creation also works:

import asyncio
import rsloop

loop = rsloop.new_event_loop()
asyncio.set_event_loop(loop)
try:
    loop.run_until_complete(...)
finally:
    asyncio.set_event_loop(None)
    loop.close()

Examples

uv run python examples/01_basics.py
uv run python examples/02_fd_and_sockets.py
uv run python examples/03_streams.py
uv run python examples/04_unix_and_accepted_socket.py
uv run python examples/05_pipes_signals_subprocesses.py

The repository also includes:

  • demo/fastapi_service.py for running the same FastAPI app on stdlib asyncio, uvloop, or rsloop
  • benchmarks/compare_event_loops.py for comparing callback, task, and TCP stream workloads

Benchmark

uv run --with maturin maturin develop --release
uv run --with uvloop python benchmarks/compare_event_loops.py

See benchmarks/README.md for workload details and extra benchmark flags.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rsloop-0.1.1.tar.gz (266.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rsloop-0.1.1-cp314-cp314t-manylinux_2_39_x86_64.whl (835.2 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl (811.7 kB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

rsloop-0.1.1-cp314-cp314-manylinux_2_39_x86_64.whl (843.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl (825.3 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

rsloop-0.1.1-cp313-cp313-manylinux_2_39_x86_64.whl (845.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl (827.4 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

rsloop-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl (846.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl (828.0 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

rsloop-0.1.1-cp311-cp311-manylinux_2_39_x86_64.whl (838.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl (825.1 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

rsloop-0.1.1-cp310-cp310-manylinux_2_39_x86_64.whl (839.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl (826.2 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

rsloop-0.1.1-cp39-cp39-manylinux_2_39_x86_64.whl (842.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp39-cp39-macosx_10_12_x86_64.whl (828.7 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

rsloop-0.1.1-cp38-cp38-manylinux_2_39_x86_64.whl (841.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.39+ x86-64

rsloop-0.1.1-cp38-cp38-macosx_10_12_x86_64.whl (827.9 kB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

Details for the file rsloop-0.1.1.tar.gz.

File metadata

  • Download URL: rsloop-0.1.1.tar.gz
  • Upload date:
  • Size: 266.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rsloop-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ce6bf3f85130c554769af62e72a0bdc3d505f75071182f10b5230a08628ebb8d
MD5 b506c043f248544e85a29a188328582d
BLAKE2b-256 2f7197c2f838256cc2003afe77fa868719f04a284da27770e884fe8e407f13c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1.tar.gz:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp314-cp314t-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp314-cp314t-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8db31499ce29b3c1bf359edd08aed06a4ca658f3f7836826f16a73b1b0bd2e70
MD5 a12697bcfdbe5b815f02e6f7ff5d7e45
BLAKE2b-256 eba999be6c2d83c40050e16fbeafaf7a1c43666f1ed5c55273fc83b9794688c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp314-cp314t-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c86dac6118c3e950c333bf830dfce76314856e208aa138271f331808f752e49c
MD5 977ea2990a1bffcc97a56ddfb685afd2
BLAKE2b-256 69e5cb50064f0dbcf83b8bf6e985f596730c9e414bd724ebe32b2ca3490cd96e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp314-cp314-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp314-cp314-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 db951032c437b487e4a4b249606b9a7c8a252b462fc83edd3ec338d256a99d9b
MD5 0e0d7d8864b4fdb6b594c25a60735d7c
BLAKE2b-256 6b0da1b9c2743288684bab092472f1e4ca96da62f2c3382f57d4486294d1207a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp314-cp314-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b81aa4ab8943bc49a25d6032047212fec0c084732b61239cc72e92344ee965fc
MD5 a96b257e5543028a0b97f0a47d281715
BLAKE2b-256 d63c7ed8226bfa82d4fb7427986287b9b62b49e2814ab44875c331e9779f9507

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8719be82c95f489ac46f6c4660a2ad750260370fe51d381b93659145b051f7e6
MD5 79644d1f15e5b0a4e0b9e6e3ab467393
BLAKE2b-256 8ba3bf5b311fd77383328d0aef238a45269f923cf48f2cf754de48e3909b23a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp313-cp313-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 86e0fbdece5d4e9ebe2958180faecdec3401f97c2010afb4b090590e5d302905
MD5 65c87649ef90c7d10840769aacd94026
BLAKE2b-256 5669e8ffca74c1e422dd09d23698cef02d018b92e19cedaa1ec8b7a0e1de86cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b25b20b2d468482b4efd556be17dda08e9c0684286bd227372cf394d8f7ebaeb
MD5 a74fac153640e776e6f6d0c992820aae
BLAKE2b-256 4a4851ac0a933b9f2c00e5608c3a8da4059aa5a1eb889ba83d4a49daa0a46378

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 66adf88c55297d4e3600d2c0ad4d424b07cd65766d8b9af60064154ff3195eaf
MD5 8e1bdde7972c55c13356db9fddf44dc6
BLAKE2b-256 e1c045076dc7739eb4de93f149f83cd9bffce6422dfdefabce7abb08d1e40fb8

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 144a898929c10eb27331a0dcc0bea64396e84f49690f61ab2ba7a130953533d9
MD5 9d0bbedc03e85e4d672dade0db6b278c
BLAKE2b-256 d5a7900e90be3436762feef212a4005a941c0b2984b21ce7ae10c12a947d1db1

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp311-cp311-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 246eb9d4b734ddef550c5ea26b9c478f3b8045b7407fad4586f30a9eec5b1ecc
MD5 cb3c66dfa4674b2ee7006bef998fb8be
BLAKE2b-256 ee5d8e7e4fab1b50dd9beb5c3d8d3fc65a286c3c58c617a9946f8f2328a758c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2dda4062cba9fd370e39eac9ae4b005bbc0e193f93d46cd9e0adf1b0a97ff493
MD5 b895adeb6f0abb8614727d4f60d20030
BLAKE2b-256 3e33cee01eb02f17c1ac261fc5342e535c9c4f8c8a45061b6427bc3a6c773690

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp310-cp310-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7e3f1ea9c95029700d5b9c509871db828d7c737bdcbed848348666bad1389f45
MD5 937f78b272e76e1a077261beabeaba56
BLAKE2b-256 6b0e08ba859ce3ea8716d94586a48a0a04fe5ac1a6e09ee417f5168cd6261ab4

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp39-cp39-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp39-cp39-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d43bde4a11ba63ac535e8cd3c8b5069ae4294f362a5fbb511e182aa5f1cfa668
MD5 daef92d8729041c1d36ce0220dd57773
BLAKE2b-256 bf105a7b522e4ee1214f1abd5c6e1adf4280483451ae21f551f674c925177111

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp39-cp39-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4534ee734f16f08ed42c428971c02b8b54e786f6abb810bf77857439bc010acc
MD5 97a7e79c9539012b61133969d25562f2
BLAKE2b-256 f275c44096982f68f0108cc1772f14b7e47f8afd035399dc6d364b34c221502a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp39-cp39-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp38-cp38-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp38-cp38-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3468ef468ee5b8f213088e1eae799f4fa3acc590ab6f20add68ecccd2c29abb7
MD5 568578dfc6983aaf5e4c85f1ca7b5bd4
BLAKE2b-256 e12436cf383234dfb6a5729ed6264a4f1690e8212d62ab5f800d24f8ffcdf978

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp38-cp38-manylinux_2_39_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rsloop-0.1.1-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsloop-0.1.1-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 917dc46909e2ebddea04db498d850ff7edb743035a480ee07445555b9030056d
MD5 cbaf4c41fb4285a1a1f68429403278cc
BLAKE2b-256 74e2762c1624a245050845bb5379b30d29c6ea490db8c8624409374025774e92

See more details on using hashes here.

Provenance

The following attestation bundles were made for rsloop-0.1.1-cp38-cp38-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on RustedBytes/rsloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page