How to open special files (procfs, sysfs, etc.)
masterLinux native AIO and io_uring backends do not support special files like /proc/, /sys/, or Unix pipes. To work with these, you must provide a compatible context object using caio.thread_aio_asyncio.AsyncioContext. This switches the implementation to a thread-based approach which is compatible with special filesystems.
Note: The custom context object should be reused if opening multiple special files.
import asyncio
from aiofile import async_open
from caio import thread_aio_asyncio
from contextlib import AsyncExitStack
async def main():
async with AsyncExitStack() as stack:
# Create and reuse a thread-based context for special files
ctx = await stack.enter_async_context(
thread_aio_asyncio.AsyncioContext()
)
# Open special file with custom context
src = await stack.enter_async_context(
async_open("/proc/cpuinfo", "r", context=ctx)
)
# Open regular file with default context
dest = await stack.enter_async_context(
async_open("/tmp/cpuinfo", "w")
)
async for line in src:
await dest.write(line)
asyncio.run(main())