Skip to main content

This project can quickly package your Python code into a service that is ready for production environments.

Project description

中文 [English]

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.

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

python_fast_service-0.1.4.tar.gz (58.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_fast_service-0.1.4-py3-none-any.whl (70.4 kB view details)

Uploaded Python 3

File details

Details for the file python_fast_service-0.1.4.tar.gz.

File metadata

  • Download URL: python_fast_service-0.1.4.tar.gz
  • Upload date:
  • Size: 58.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.20

File hashes

Hashes for python_fast_service-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d00e5c469f45396674d0664c9e7534c96c6ebd67681911615f129063d7694467
MD5 d683c77a74b7a59160faa10ab652e961
BLAKE2b-256 db87bdee597b909b73a4290c7f227e12ad6595fe60f291f4240a779ba59ffa9c

See more details on using hashes here.

File details

Details for the file python_fast_service-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for python_fast_service-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 681c61f6b7cda16a80117161b55dc03ee9daca16763a2e70ed18f35a1e4c871c
MD5 a590a1f7dfec4a76abaee68479e31909
BLAKE2b-256 b1c0c6102a1a4532b7be1015f79d20c8592533aa0cadd0becaa9624a84245921

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page