Skip to main content

Python serverless framework for Datastone Spirit GPU.

Project description

Spirit-GPU

Install

pip install spirit-gpu

Usage example

from spirit_gpu import start
from spirit_gpu.env import Env
from typing import Dict, Any

def handler(request: Dict[str, Any], env: Env):
    """
    request: Dict[str, Any], from client http request body.
    request["input"]: Required.
    request["webhook"]: Optional string for asynchronous requests.

    returned object to be serialized into JSON and sent to the client.
    in this case: '{"output": "hello"}'
    """
    return {"output": "hello"}


def gen_handler(request: Dict[str, Any], env: Env):
    """
    append yield output to array, serialize into JSON and send to client.
    in this case: [0, 1, 2, 3, 4]
    """
    for i in range(5):
        yield i


async def async_handler(request: Dict[str, Any], env: Env):
    """
    returned object to be serialized into JSON and sent to the client.
    """
    return {"output": "hello"}


async def async_gen_handler(request: Dict[str, Any], env: Env):
    """
    append yield output to array, serialize into JSON and send to client.
    """
    for i in range(10):
        yield i


def concurrency_modifier(current_allowed_concurrency: int) -> int:
    """
    Adjusts the allowed concurrency level based on the current state.
    For example, if the current allowed concurrency is 3 and resources are sufficient,
    it can be increased to 5, allowing 5 tasks to run concurrently.
    """
    allowed_concurrency = ...
    return allowed_concurrency


"""
Register the handler with serverless.start().
Handlers can be synchronous, asynchronous, generators, or asynchronous generators.
"""
start({
    "handler": async_handler, "concurrency_modifier": concurrency_modifier
})

Logging

We provide a tool to log information. Default logging level is "INFO", you can call logger.set_level(logging.DEBUG) to change it.

Please make sure you update to the latest version to use this feature.

from spirit_gpu import start, logger
from spirit_gpu.env import Env
from typing import Dict, Any


def handler(request: Dict[str, Any], env: Env):
    """
    request: Dict[str, Any], from client http request body.
    request["input"]: Required.
    request["webhook"]: Optional string for asynchronous requests.

    we will only add request["meta"]["requestID"] if it not exist in your request.
    """
    request_id = request["meta"]["requestID"]
    logger.info("start to handle", request_id = request_id, caller=True)
    return {"output": "hello"}

start({"handler": handler})

API

Please read API or 中文 API for how to use spirit-gpu serverless apis and some other import policies.

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

spirit_gpu-0.0.5.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

spirit_gpu-0.0.5-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file spirit_gpu-0.0.5.tar.gz.

File metadata

  • Download URL: spirit_gpu-0.0.5.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for spirit_gpu-0.0.5.tar.gz
Algorithm Hash digest
SHA256 1a2da70f89f8b394fb1b26dcf23e0011b1183d9b9620344b088922526f86a29f
MD5 7977cc68cac56b8d2d64b8d52e790c50
BLAKE2b-256 f0e34e898adee2a488aed2fb9ad56fe6a1015f5b397124db99c46b917d04c074

See more details on using hashes here.

File details

Details for the file spirit_gpu-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: spirit_gpu-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for spirit_gpu-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 82e0f1d4460f75d2e597c7a61210f1eb653da612c7a1c4ecb80cea09c688d18d
MD5 dd6d89ba5794526184e4a0659a0cd8d6
BLAKE2b-256 e4e2d5d85343f0460720d787b92ace147da0881eb9839a0d10f8a0f9104e58af

See more details on using hashes here.

Supported by

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