Skip to main content

slimfaas-client

Python client to connect Jobs or virtual functions to SlimFaas via WebSocket. Lets any process receive async requests and publish/subscribe events without exposing an HTTP port.

PyPI PyPI Downloads Python Versions GitHub Website

Links:

Requirements

  • Python ≥ 3.10
  • UV as package manager

Installation

uv add slimfaas-client
# or from source
uv pip install -e .

Quick start

import asyncio
from slimfaas_client import (
    SlimFaasClient, SlimFaasClientConfig,
    SubscribeEventConfig, FunctionVisibility,
    AsyncRequest, PublishEvent,
)

async def handle_request(req: AsyncRequest) -> int:
    """Called when SlimFaas sends an async-function request.
    Return an HTTP status code (200 = success, 500 = error, 202 = long processing)."""
    print(f"{req.method} {req.path}{req.query}")
    print(f"Body: {req.body}")
    return 200

async def handle_event(evt: PublishEvent) -> None:
    """Called when SlimFaas publishes a publish-event."""
    print(f"Event '{evt.event_name}': {evt.body}")

async def main():
    config = SlimFaasClientConfig(
        function_name="my-job",
        subscribe_events=[
            SubscribeEventConfig(name="order-created"),
            SubscribeEventConfig(name="order-updated"),
        ],
        default_visibility=FunctionVisibility.PUBLIC,
        number_parallel_request=5,
    )

    async with SlimFaasClient("ws://slimfaas:5003/ws", config) as client:
        client.on_async_request(handle_request)
        client.on_publish_event(handle_event)
        await client.run_forever()

asyncio.run(main())

Full configuration

from slimfaas_client import (
    SlimFaasClientConfig, SubscribeEventConfig, PathVisibilityConfig,
    FunctionVisibility, FunctionTrust,
)

config = SlimFaasClientConfig(
    function_name="my-job",

    # SlimFaas/DependsOn
    depends_on=["other-function"],

    # SlimFaas/SubscribeEvents — each entry may override visibility individually
    subscribe_events=[
        SubscribeEventConfig(name="my-event", visibility=FunctionVisibility.PUBLIC),
        SubscribeEventConfig(name="internal-event"),  # inherits default_visibility
    ],

    # SlimFaas/DefaultVisibility
    default_visibility=FunctionVisibility.PUBLIC,  # or PRIVATE

    # SlimFaas/PathsStartWithVisibility
    paths_start_with_visibility=[
        PathVisibilityConfig(path="/admin", visibility=FunctionVisibility.PRIVATE),
    ],

    # SlimFaas/Configuration
    configuration='{"key": "value"}',

    # SlimFaas/ReplicasStartAsSoonAsOneFunctionRetrieveARequest
    replicas_start_as_soon_as_one_function_retrieve_a_request=True,

    # SlimFaas/NumberParallelRequest
    number_parallel_request=10,

    # SlimFaas/NumberParallelRequestPerPod
    number_parallel_request_per_pod=5,

    # SlimFaas/DefaultTrust
    default_trust=FunctionTrust.TRUSTED,  # or UNTRUSTED
)

Sync streaming (HTTP-over-WebSocket)

from slimfaas_client import SyncRequest

async def handle_sync(req: SyncRequest) -> None:
    body = b'{"status": "ok"}'
    await req.response.start(200, {"Content-Type": ["application/json"]})
    await req.response.write(body)
    await req.response.complete()

client.on_sync_request(handle_sync)

Long-running requests (status 202)

Return 202 to acknowledge the request without completing it yet, then call send_callback when done:

async def handle_long(req: AsyncRequest) -> int:
    asyncio.create_task(process_in_background(req))
    return 202  # "I'll handle it — will call back"

async def process_in_background(req: AsyncRequest) -> None:
    await asyncio.sleep(10)
    await client.send_callback(req.element_id, 200)

Dependency injection

The handlers are plain async functions, so you can close over any dependency you resolved from your DI framework:

# Example with a database session from SQLAlchemy
from sqlalchemy.ext.asyncio import AsyncSession

async def make_handler(session: AsyncSession):
    async def handle_request(req: AsyncRequest) -> int:
        await session.execute(...)  # use the injected session
        return 200
    return handle_request

client.on_async_request(await make_handler(db_session))

Automatic reconnection

The client reconnects automatically after a disconnection. Configure the delay between attempts and the keepalive ping interval:

client = SlimFaasClient(
    "ws://...",
    config,
    reconnect_delay=10.0,
    ping_interval=30.0,  # use 0 to disable keepalive pings
)

Important rules

  1. function_name must not match an existing Kubernetes Deployment name. SlimFaas will reject the registration with a SlimFaasRegistrationError.

  2. All clients sharing the same function_name must have the exact same configuration. Mismatches are rejected on connection.

Development

uv sync --extra dev
uv run pytest

Release files for slimfaas-client 0.84.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slimfaas-client 0.84.10
File Size Uploaded
slimfaas_client-0.84.10.tar.gz 79.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slimfaas-client 0.84.10
File Interpreter ABI Platform
slimfaas_client-0.84.10-py3-none-any.whl Python 3 none any Details

Total release size: 92.9 kB

Release files / slimfaas_client-0.84.10.tar.gz

Download URL slimfaas_client-0.84.10.tar.gz
Size 79.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0a9bfd51c150a911138adb99a9a81cfa958270619401dea5d888f858adc8b99c
BLAKE2b-256 checksum
How to use checksums
8f30344598c28db20e405d2e93c1097925377844c4d5597abcc9a6fba2042365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / slimfaas_client-0.84.10-py3-none-any.whl

Download URL slimfaas_client-0.84.10-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2068731c7c612255a5a06f1ed3fff19a907ce6561004925e55f2982fc043fcc2
BLAKE2b-256 checksum
How to use checksums
10a82f051410a1c7ec989796083e5ccc1d967973bb15e33201cf68ea30baa662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.84.10 This release

2 release files

0.84.9

2 release files

0.84.8

2 release files

0.84.7

2 release files

0.84.6

2 release files

0.84.5

2 release files

0.84.4

2 release files

0.84.3

2 release files

0.84.2

2 release files

0.84.1

2 release files

0.84.0

2 release files

0.82.0

2 release files

0.81.1

2 release files

0.79.2

2 release files

0.79.1

2 release files

0.79.0

2 release files

0.78.0

2 release files

0.77.1

2 release files

0.77.0

2 release files

0.76.1

2 release files

0.76.0

2 release files

0.75.0

2 release files

0.74.8

2 release files

0.74.7

2 release files

0.74.6

2 release files

0.74.5

2 release files

0.74.4

2 release files

0.74.3

2 release files

0.74.2

2 release files

0.74.1

2 release files

0.72.2

2 release files

0.72.1

2 release files

0.72.0

2 release files

0.71.3

2 release files

0.71.2

2 release files

0.71.1

2 release files

0.71.0

2 release files

0.70.2

2 release files

0.69.0

2 release files

0.68.0

2 release files

0.67.0

2 release files

0.66.5

2 release files

0.66.4

2 release files

0.66.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page