| 1 |
Python wrapper for AIO |
| 2 |
====================== |
| 3 |
|
| 4 |
Python bindings for async file I/O on Linux and a pure-Python/thread fallback |
| 5 |
for other platforms. |
| 6 |
|
| 7 |
Three backends are available: |
| 8 |
|
| 9 |
| Backend | Kernel | Notes | |
| 10 |
|-----------------|--------|------------------------------------------------------------| |
| 11 |
| `linux_uring` | ≥ 5.6 | io_uring - shared ring buffers, zero-syscall completions | |
| 12 |
| `linux_aio` | ≥ 4.18 | kernel AIO (`io_submit` / `io_getevents`), `O_DIRECT` only | |
| 13 |
| `thread_aio` | any | pthreads pool, portable | |
| 14 |
| `python_aio` | any | pure Python, no C extension required | |
| 15 |
|
| 16 |
Example |
| 17 |
------- |
| 18 |
|
| 19 |
```python |
| 20 |
import asyncio |
| 21 |
from caio import AsyncioContext |
| 22 |
|
| 23 |
loop = asyncio.get_event_loop() |
| 24 |
|
| 25 |
async def main(): |
| 26 |
# max_requests=128 by default |
| 27 |
ctx = AsyncioContext(max_requests=128) |
| 28 |
|
| 29 |
with open("test.file", "wb+") as fp: |
| 30 |
fd = fp.fileno() |
| 31 |
|
| 32 |
# Execute one write operation |
| 33 |
await ctx.write(b"Hello world", fd, offset=0) |
| 34 |
|
| 35 |
# Execute one read operation |
| 36 |
print(await ctx.read(32, fd, offset=0)) |
| 37 |
|
| 38 |
# Execute one fdsync operation |
| 39 |
await ctx.fdsync(fd) |
| 40 |
|
| 41 |
op1 = ctx.write(b"Hello from ", fd, offset=0) |
| 42 |
op2 = ctx.write(b"async world", fd, offset=11) |
| 43 |
|
| 44 |
await asyncio.gather(op1, op2) |
| 45 |
|
| 46 |
print(await ctx.read(32, fd, offset=0)) |
| 47 |
# Hello from async world |
| 48 |
|
| 49 |
|
| 50 |
loop.run_until_complete(main()) |
| 51 |
``` |
| 52 |
|
| 53 |
Selecting a backend |
| 54 |
------------------- |
| 55 |
|
| 56 |
`from caio import AsyncioContext` picks the best available backend automatically |
| 57 |
(`linux_uring` → `linux_aio` → `thread_aio` → `python_aio`). |
| 58 |
|
| 59 |
To force a specific backend use the `CAIO_IMPL` environment variable: |
| 60 |
|
| 61 |
```bash |
| 62 |
CAIO_IMPL=uring python my_app.py # linux_uring |
| 63 |
CAIO_IMPL=linux python my_app.py # linux_aio |
| 64 |
CAIO_IMPL=thread python my_app.py # thread_aio |
| 65 |
CAIO_IMPL=python python my_app.py # python_aio |
| 66 |
``` |
| 67 |
|
| 68 |
Or import a backend directly: |
| 69 |
|
| 70 |
```python |
| 71 |
# io_uring (Linux ≥ 5.6) |
| 72 |
from caio.linux_uring_asyncio import AsyncioContext |
| 73 |
|
| 74 |
# kernel AIO (Linux ≥ 4.18) |
| 75 |
from caio.linux_aio_asyncio import AsyncioContext |
| 76 |
|
| 77 |
# thread pool |
| 78 |
from caio.thread_aio_asyncio import AsyncioContext |
| 79 |
``` |
| 80 |
|
| 81 |
A `default_implementation` file placed next to `caio/__init__.py` (useful for |
| 82 |
distro package maintainers) may contain one of `uring`, `linux`, `thread`, or |
| 83 |
`python` on its first non-comment line. |
| 84 |
|
| 85 |
Troubleshooting |
| 86 |
--------------- |
| 87 |
|
| 88 |
### io_uring blocked by seccomp |
| 89 |
|
| 90 |
Containers (Docker, Podman, Kubernetes) often block io_uring via a seccomp |
| 91 |
filter. The import will raise `ImportError` with a diagnostic message if |
| 92 |
`io_uring_setup(2)` returns `ENOSYS`. |
| 93 |
|
| 94 |
Fix: |
| 95 |
|
| 96 |
```bash |
| 97 |
# Docker / Podman |
| 98 |
docker run --security-opt seccomp=unconfined ... |
| 99 |
|
| 100 |
# Kubernetes |
| 101 |
securityContext: |
| 102 |
seccompProfile: |
| 103 |
type: Unconfined |
| 104 |
``` |
| 105 |
|
| 106 |
### linux_aio compatibility |
| 107 |
|
| 108 |
`linux_aio` requires kernel ≥ 4.18 and a compatible filesystem. If it does |
| 109 |
not work in your environment you can fall back to `thread` or `python`: |
| 110 |
|
| 111 |
```bash |
| 112 |
CAIO_IMPL=thread python my_app.py |
| 113 |
``` |