Skip to main content

Official Python SDK for Rune — protocol-first multi-language function execution framework

Project description

Rune Python SDK

Official Python Caster SDK for the Rune framework.

安装

pip install rune-framework

要求 Python >= 3.10。

快速开始

from rune import Caster

caster = Caster("localhost:50070")

@caster.rune("echo", gate="/echo")
async def echo(ctx, input):
    return input

caster.run()

启动后 Runtime 自动暴露:

POST /echo              -- sync
POST /echo?stream=true  -- SSE streaming
POST /echo?async=true   -- async task

Caster 配置

caster = Caster(
    addr="localhost:50070",           # Runtime gRPC 地址
    caster_id="my-python-caster",     # Caster ID(默认 "python-caster")
    max_concurrent=10,                # 最大并发处理数
    labels={"env": "prod", "gpu": "true"},  # Caster 标签(用于标签路由)
    api_key="rk_xxx",                # API Key(认证开启时需要)
    reconnect_base_delay=1.0,         # 重连初始延迟(秒)
    reconnect_max_delay=30.0,         # 重连最大延迟(秒)
    heartbeat_interval=10.0,          # 心跳间隔(秒)
)

注册 Rune

Unary Handler

@caster.rune(
    "translate",
    gate="/translate",                # HTTP 路由路径
    gate_method="POST",               # HTTP 方法(默认 POST)
    version="1.0.0",                  # 语义版本
    description="Translate text",     # 人类可读描述
    priority=10,                      # 优先级(越高越优先)
    input_schema={                    # JSON Schema 输入校验
        "type": "object",
        "properties": {
            "text": {"type": "string"},
            "lang": {"type": "string"}
        },
        "required": ["text", "lang"]
    },
    output_schema={                   # JSON Schema 输出校验
        "type": "object",
        "properties": {
            "translated": {"type": "string"}
        }
    },
)
async def translate(ctx, input):
    data = json.loads(input)
    result = do_translate(data["text"], data["lang"])
    return json.dumps({"translated": result})

Streaming Handler

@caster.stream_rune("generate", gate="/generate")
async def generate(ctx, input, sender):
    prompt = json.loads(input)["prompt"]
    for token in model.stream(prompt):
        await sender.emit(token)       # 发送一个 chunk
    # StreamEnd 自动发送

StreamSender.emit() 接受 bytesstrdictlist 类型,自动序列化。

File Handler

Handler 的第三个参数命名为 files 时,自动接收文件附件:

@caster.rune("process-doc", gate="/process")
async def process_doc(ctx, input, files):
    # files: list[FileAttachment]
    for f in files:
        print(f.filename, f.mime_type, len(f.data))
    return json.dumps({"processed": len(files)})

FileAttachment 字段:

  • filename: str -- 文件名
  • data: bytes -- 文件内容
  • mime_type: str -- MIME 类型

Context

每次调用都会传入 RuneContext

@caster.rune("example", gate="/example")
async def example(ctx, input):
    print(ctx.rune_name)     # "example"
    print(ctx.request_id)    # "r-18e8f3a1b-0"
    print(ctx.context)       # {} (额外的 key-value 上下文)
    return input

运行与停止

# 阻塞运行(自动重连)
caster.run()

# 从另一个线程停止
caster.stop()

开发

cd sdks/python
pip install -e ".[dev]"
pytest tests/test_unit.py       # 单元测试
pytest tests/test_e2e.py -m e2e # E2E 测试(需要运行中的 Runtime)

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

rune_framework-1.1.1.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

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

rune_framework-1.1.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file rune_framework-1.1.1.tar.gz.

File metadata

  • Download URL: rune_framework-1.1.1.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for rune_framework-1.1.1.tar.gz
Algorithm Hash digest
SHA256 af39ef3eaebb0970a59e26a6544d7597c225bf3c8a4250f32e557e683408ace0
MD5 1ff0c979638061769ba3be61c61f1e2f
BLAKE2b-256 608ec984a60554d5de5f084623b74eec3e01ca6e67ae08a80c66101e0f51ed76

See more details on using hashes here.

File details

Details for the file rune_framework-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: rune_framework-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for rune_framework-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 53b8b145a54fa6565a8667aa1bc75f933c0ff460a0620534a8adffe0f9f830d1
MD5 64b1ddb1b1301553cc28864035bb7fe7
BLAKE2b-256 c1e164d35eb7c9f3e6c0890d321eabd41f5f959d4ce26438fb4adde32b01f957

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