Skip to main content

FunctionStream SDK is a powerful Python library for building and deploying serverless streaming functions that runs on Function Stream platform.

Project description

FunctionStream Python SDK

FunctionStream SDK is a powerful Python library for building and deploying serverless functions that process messages from Apache Pulsar. It provides a simple yet flexible framework for creating event-driven applications with robust error handling, metrics collection, and resource management.

Features

  • Easy Function Development: Simple API for creating serverless functions
  • Message Processing: Built-in support for Apache Pulsar message processing
  • Metrics Collection: Automatic collection of performance metrics
  • Resource Management: Efficient handling of connections and resources
  • Configuration Management: Flexible configuration through YAML files
  • Error Handling: Comprehensive error handling and logging

Installation

pip install function-stream

Quick Start

  1. Create a function that processes messages:
from function_stream import FSFunction

async def my_process_function(request_data: dict) -> dict:
    # Process the request data
    result = process_data(request_data)
    return {"result": result}

# Initialize and run the function
function = FSFunction(
    process_funcs={
        'my_module': my_process_function
    }
)

await function.start()
  1. Create a configuration file (config.yaml):
pulsar:
  service_url: "pulsar://localhost:6650"
  authPlugin: ""  # Optional
  authParams: ""  # Optional

module: "my_module"
subscriptionName: "my-subscription"

requestSource:
  - pulsar:
      topic: "input-topic"

sink:
  pulsar:
    topic: "output-topic"
  1. Define your function package (package.yaml):
name: my_function
type: pulsar
modules:
  my_module:
    name: my_process
    description: "Process incoming messages"
    inputSchema:
      type: object
      properties:
        data:
          type: string
      required:
        - data
    outputSchema:
      type: object
      properties:
        result:
          type: string

Core Components

FSFunction

The main class for creating serverless functions. It handles:

  • Message consumption and processing
  • Response generation
  • Resource management
  • Metrics collection
  • Error handling

Configuration

The SDK uses YAML configuration files to define:

  • Pulsar connection settings
  • Module selection
  • Topic subscriptions
  • Input/output topics
  • Custom configuration parameters

Metrics

Built-in metrics collection for:

  • Request processing time
  • Success/failure rates
  • Message throughput
  • Resource utilization

Examples

Check out the examples directory for complete examples:

  • string_function.py: A simple string processing function
  • test_string_function.py: Test client for the string function
  • config.yaml: Example configuration
  • package.yaml: Example package definition

Best Practices

  1. Error Handling

    • Always handle exceptions in your process functions
    • Use proper logging for debugging
    • Implement graceful shutdown
  2. Resource Management

    • Close resources properly
    • Use context managers when possible
    • Monitor resource usage
  3. Configuration

    • Use environment variables for sensitive data
    • Validate configuration values
    • Document configuration options
  4. Testing

    • Write unit tests for your functions
    • Test error scenarios
    • Validate input/output schemas

Development

Prerequisites

  • Python 3.7+
  • Apache Pulsar
  • pip

Setup Development Environment

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
.\venv\Scripts\activate  # Windows

# Install dependencies
pip install -r requirements.txt

# Install the package in development mode
python -m pip install -e .

Running Tests

make test

Support

For support, please open an issue in the GitHub repository or contact the maintainers.

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

function_stream-0.6.0rc1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

function_stream-0.6.0rc1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file function_stream-0.6.0rc1.tar.gz.

File metadata

  • Download URL: function_stream-0.6.0rc1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.18

File hashes

Hashes for function_stream-0.6.0rc1.tar.gz
Algorithm Hash digest
SHA256 2f3b6a103e8b3eebf0d9a5fc735402f91a16bac9d0812cce729361b68c75225c
MD5 7c919c90d8ccd33d33afc5ec9b6202db
BLAKE2b-256 f8dbdd2d0e361518b7bb78b079caf8f71651bc4edbb5979393f922da221fdd43

See more details on using hashes here.

File details

Details for the file function_stream-0.6.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for function_stream-0.6.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 d859071ae72096d000de77c064cafd05a595557cba29f74bba828f5a8f3fde78
MD5 aed7f95dbc671b59a804195bf1753ad2
BLAKE2b-256 15482104292bfe61e69b143dd243c79b7b580f562d67089b698eaa0d4dd58708

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