{"id":13743399,"url":"https://github.com/metacall/python-c-io_uring-example","last_synced_at":"2025-12-27T05:18:29.075Z","repository":{"id":104919556,"uuid":"431212092","full_name":"metacall/python-c-io_uring-example","owner":"metacall","description":"Using io_uring Linux Kernel interface from Python by JITing C code with MetaCall.","archived":false,"fork":false,"pushed_at":"2021-11-23T18:31:31.000Z","size":10,"stargazers_count":27,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-22T15:30:58.700Z","etag":null,"topics":["c","ffi","io-uring","iteroperability","jit","liburing","python"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metacall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-11-23T18:30:17.000Z","updated_at":"2024-05-14T00:23:58.000Z","dependencies_parsed_at":"2023-05-27T02:45:08.457Z","dependency_job_id":null,"html_url":"https://github.com/metacall/python-c-io_uring-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metacall%2Fpython-c-io_uring-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metacall%2Fpython-c-io_uring-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metacall%2Fpython-c-io_uring-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metacall%2Fpython-c-io_uring-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metacall","download_url":"https://codeload.github.com/metacall/python-c-io_uring-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224796363,"owners_count":17371479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","ffi","io-uring","iteroperability","jit","liburing","python"],"created_at":"2024-08-03T05:00:46.085Z","updated_at":"2025-12-27T05:18:24.035Z","avatar_url":"https://github.com/metacall.png","language":"C","readme":"# [MetaCall](https://github.com/metacall/core) Python C `io_uring` Example\n\n[`io_uring`](https://en.wikipedia.org/wiki/Io_uring) is a new Linux Kernel interface that speeds up I/O operations in comparison to previous implementations like [`libuv`](https://libuv.org/) (the library that NodeJS uses internally for handling I/O).\n\nThis interface is offered through [`liburing`](https://github.com/axboe/liburing), which provides a C API for accessing it. We could write a Python extension by using [`Python C API`](https://docs.python.org/3/c-api/index.html), or use [`ctypes`](https://docs.python.org/3/library/ctypes.html) in order to call to the library.\n\nDeveloping those wrappers is costly because either we have to write C/C++ or Python boilerplate. So instead of doing that, we will be using [MetaCall](https://github.com/metacall/core) in order to achieve this.\n\nMetaCall allows to transparently call to C functions, we can implement anything in C and without need to compile it manually (it will be JITed at load time). So basically we can load C code directly into Python. For example:\n\n```c\nint sum(int a, int b) {\n\treturn a + b;\n}\n```\n\n```py\nfrom sum.c import sum\n\nsum(3, 4) # 7\n```\n\nWith this we can use our C library not only from Python but from any other language, for example, NodeJS:\n\n```js\nconst { sum } = require(\"./sum.c\");\n\nsum(3, 4) // 7\n```\n\nWe will be avoiding all the boilerplate and we will have a single interface for all languages. The calls will be also type safe and we will avoid a lot of errors and time for maintaining the wrappers for each language that we can spend focusing on the development. Here's an example of [Cython + LWAN](https://www.nexedi.com/NXD-Blog.Multicore.Python.HTTP.Server), as you can see, although the performance is excellent, the boilerplate is huge in comparison to MetaCall.\n\nIn this example we want to bring the power of `io_uring` to Python for maximizing the speed of I/O and outperform Python native primitives like `http.server` module. For demonstrating it, we have a `server_listen` function which creates a simple HTTP server in the port `8000`. As example, we are also passing another parameter as a callback called `fibonacci`. This is a function that will act as a handler for the POST method, we will use it for calculating the fibonacci sequence. When doing a POST against the server, we will use the path as the Nth element of fibonacci sequence to be calculated. In our C code this will get translated automatically into a function pointer with the signature `long (*handler)(long)` transparently. Invoking the callback `handler` from C land will execute the function `fibonacci` in Python land.\n\n## Docker\n\nBuilding and running with Docker:\n\n```bash\ndocker build -t metacall/python-c-io_uring-example .\ndocker run --rm -p 8000:8000 -it metacall/python-c-io_uring-example\n```\n\n## Testing\n\nFor calculating the Fibonacci of 15 and 17 respectively:\n\n```bash\ncurl -X POST http://localhost:8000/15\n610\ncurl -X POST http://localhost:8000/17\n1597\n```\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Python"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetacall%2Fpython-c-io_uring-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetacall%2Fpython-c-io_uring-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetacall%2Fpython-c-io_uring-example/lists"}