{"id":19371677,"url":"https://github.com/difhel/cython-fastapi-example","last_synced_at":"2025-04-23T16:31:24.921Z","repository":{"id":219107323,"uuid":"692360876","full_name":"difhel/cython-fastapi-example","owner":"difhel","description":"An example of using a cythonized project on FastAPI with assembly into a single binary executable","archived":false,"fork":false,"pushed_at":"2024-01-25T13:13:26.000Z","size":7,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-01-25T14:33:02.742Z","etag":null,"topics":["cython","fastapi","fastapi-template","python","python-api"],"latest_commit_sha":null,"homepage":"https://difhel.dev/blog/cython-fastapi-benchmark","language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/difhel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-09-16T08:41:29.000Z","updated_at":"2024-01-25T14:33:06.574Z","dependencies_parsed_at":"2024-01-25T14:43:10.236Z","dependency_job_id":null,"html_url":"https://github.com/difhel/cython-fastapi-example","commit_stats":null,"previous_names":["difhel/cython-fastapi-example"],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/difhel%2Fcython-fastapi-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/difhel%2Fcython-fastapi-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/difhel%2Fcython-fastapi-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/difhel%2Fcython-fastapi-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/difhel","download_url":"https://codeload.github.com/difhel/cython-fastapi-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223928042,"owners_count":17226651,"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":["cython","fastapi","fastapi-template","python","python-api"],"created_at":"2024-11-10T08:19:30.746Z","updated_at":"2024-11-10T08:19:30.829Z","avatar_url":"https://github.com/difhel.png","language":"Makefile","readme":"# Cython FastAPI template\n\u003cp align=\"center\"\u003e\n   \u003cimg src=\"https://img.shields.io/badge/python-3670A0?style=flat\u0026logo=python\u0026logoColor=ffdd54\" alt=\"Python\"/\u003e\n   \u003cimg src=\"https://img.shields.io/badge/FastAPI-005571?style=flat\u0026logo=fastapi\" alt=\"FastAPI\"/\u003e\n   \u003cimg src=\"https://img.shields.io/badge/c-%2300599C.svg?style=flat\u0026logo=c\u0026logoColor=white\" alt=\"С\"/\u003e\n\u003c/p\u003e\n\n---\n\nA sample Python API written on [FastAPI](https://fastapi.tiangolo.com/) with helping modules for compiling into a single binary using [Cython](https://cython.org/)-ization and Python/C API.\n\nBased on [perfomance testing](https://difhel.dev/blog/cython-fastapi-benchmark#%D1%80%D0%B5%D0%B7%D1%83%D0%BB%D1%8C%D1%82%D0%B0%D1%82%D1%8B), Cython can speed up your project by **3.35 times**.\n\nYou can check my article about cythonized FastAPI projects with benchmarks (on Russian) [on my blog - difhel.dev/blog/cython-fastapi-benchmark](https://difhel.dev/blog/cython-fastapi-benchmark). This article also contains some comprehensive instructions how to build and run this code.\n\n## Project structure\n- `main.pyx` - main file, code written on Python (without Cython-flavored feautures), the entry point of the project.\n- `routes` - the routes of API (in this example only one router - `ftl` with method `/method/ftl.test`)\n- `builder.c` - C code for compiling file `main.c`. `main.c` is auto-generated C-code generated by Cython. This file is a build artifact produced by cythonizing `main.pyx`, the source code.\n- `Makefile` - makefile for the project. Options:\n   - cythonize - runs `cython main.pyx`, cythonizing source code.\n   - build - translates the project to `main.c` and compiles it to `main.\u003carch\u003e.so` shared library file\n   - gcc - compiles the file `main.c` to `app.o`\n   - run - start the project with environment variable `PYTHONPATH=pwd` (needed for Python API)\n   - clean - delete build artifacts\n\n## Dependencies and requirements\n- You are using OS Linux with the bash shell.\n- To be honest, this project does not contain any platform-specific things, so you can run it on Windows either, but you should install a C compiler. In case you are using Windows, you can try [MinGW compiler](https://www.mingw-w64.org/). You will also need [GNU make for Windows](https://gnuwin32.sourceforge.net/packages/make.htm) (of course, unless you want to copy and paste all the commands from the Makefile directly).\n### Python libraries\n- Of course, you should have FastAPI installed (`pip3 install fastapi`)\n- You should have an ASGI server. In this project cythonized version of [uvicorn](https://www.uvicorn.org/) is used. (`pip3 install \"uvicorn[standart]\"`)\n- Make sure you have Cython installed (`pip3 install cython`)\n### C compiler\n- You can use any C compiler that can build Python/C API code, but I recommend you to use standart `gcc` compiler. (`sudo apt-get install gcc`)\n\n## Run the project\n```bash\nmake \u0026\u0026 make run\n```\n\n## Check the server\n```bash\n$ curl -i http://localhost:5000/method/ftl.test\nHTTP/1.1 200 OK\ndate: Sat, 16 Sep 2023 08:58:08 GMT\nserver: uvicorn\ncontent-length: 16\ncontent-type: application/json\n\n{\"ping\":\"pong2\"}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdifhel%2Fcython-fastapi-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdifhel%2Fcython-fastapi-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdifhel%2Fcython-fastapi-example/lists"}