This project can quickly package your Python code into a service that is ready for production environments.
Project description
Introduction
This project can quickly package your Python code into a production-ready service, providing the following main features:
- Implements API interfaces in a unified manner with standardized request and response formats
- Provides useful features like monitoring, logging, and multi-environment configuration
- Supports requirements like encryption and licensing for private delivery
- Assists in building Docker images
This project was originally developed as the algorithm-base framework by my colleagues and me while working at Alibaba Cloud. Due to the need to meet commercial project requirements, the framework included redundant logic. After leaving Alibaba Cloud, I forked the project, retained the most commonly used features, and made certain optimizations.
Quick Start
Here is a minimal example to build an image for your API and deploy the service.
Install the Framework
- The current framework only supports MacOS and Linux on X86 architecture
- It has only been tested with Python 3.8
- See Installation for details
pip install python-fast-service
Write a Hello World Service
Navigate to the examples/simple directory. This serves as the template for creating future projects, as well as the Hello World program.
For the simple project, you need to implement your API (the controller layer of the service) in the api directory. The example provides a demo.py file with several API implementations. In the following code, the method decorated with @api will be automatically exposed as a RESTful API with the path /api/add. See Service and API for more details.
from ab.core import api
@api()
def add(a: int, b: int) -> int:
"""
A simple addition algorithm example
:param a: First parameter
:param b: Second parameter
:return:
"""
return a + b
Start the Service and Test
In the simple root directory, ensure port 8000 is free, and start the service by entering the following command:
pfs
After the service starts successfully, you will see output similar to the following, indicating that the service has started:
[2023-02-01 13:07:33 +0800] [12257] [INFO] Starting gunicorn 20.0.4
[2023-02-01 13:07:33 +0800] [12257] [DEBUG] Arbiter booted
[2023-02-01 13:07:33 +0800] [12257] [INFO] Listening at: http://0.0.0.0:8000 (12257)
[2023-02-01 13:07:33 +0800] [12257] [INFO] Using worker: sync
[2023-02-01 13:07:33 +0800] [12267] [INFO] Booting worker with pid: 12267
[2023-02-01 13:07:33] [12267] [DEBUG] algorithms: {('add', 'python'): add(a: int, b: int) -> int,
[2023-02-01 13:07:33] [12267] [DEBUG] fixtures: {}
[2023-02-01 13:07:33 +0800] [12257] [DEBUG] 2 workers
[2023-02-01 13:07:33] [12268] [DEBUG] algorithms: {('add', 'python'): add(a: int, b: int) -> int,
[2023-02-01 13:07:33] [12268] [DEBUG] fixtures: {}
You can access the previously defined API with the following command:
curl --location --request POST 'localhost:8000/api/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"args": {"a": 1, "b": 2}
}'
The output below shows the result of the addition algorithm:
{"code":0,"data":3}
Modifying the API Path
The Fast Service framework defaults to exposing APIs under the /api path, but you can add new paths as well. You need to create a Python module, such as endpoint.py, in the api folder in the project’s root directory, allowing you to access this API at /api/document/add.
from ab.endpoint.registry import register_endpoint
register_endpoint('/api/document/<string:api_name>')
Customizing the Response Structure
You can return a Flask Response object to replace the default Fast Service response structure. See Custom Response Structure for details
from flask import Response
return Response(f"Hello, {a - b}", status=200, mimetype='text/plain')
Build Docker Image
In the simple project root directory, enter the following command:
sh build.sh
At this point, you should have a basic understanding of the Fast Service framework. For detailed documentation, see the User Guide.
- Installation
- Service
- Performance
- OPS
- Best Practices
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_fast_service-0.2.2.tar.gz.
File metadata
- Download URL: python_fast_service-0.2.2.tar.gz
- Upload date:
- Size: 59.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7159fab0cb90726a2c615279b972a627668b54c66085f0b3182aff441fe99f0
|
|
| MD5 |
2622b3d799d3c7f7c9760a29af5de736
|
|
| BLAKE2b-256 |
9675781ef5cacad0a2b4174c309168c4a55ea1dcc1a9f9f3a7e74e66552545bf
|
File details
Details for the file python_fast_service-0.2.2-py3-none-any.whl.
File metadata
- Download URL: python_fast_service-0.2.2-py3-none-any.whl
- Upload date:
- Size: 70.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
780445c113fda3019152e2538c99013f142cef68d18e812c106e04da96f74a50
|
|
| MD5 |
dd5af9cb990c175a7b74757ecfa50211
|
|
| BLAKE2b-256 |
6d30b0fa49c964bfa1b81b9ed811fdcc735303e0f72235ab9f1469a1a394f3a9
|