dbos-python

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DBOS Python Best Practices

DBOS Python 最佳实践

Guide for building reliable, fault-tolerant Python applications with DBOS durable workflows.
本指南介绍如何使用DBOS持久化工作流构建具备可靠性、容错能力的Python应用。

When to Apply

适用场景

Reference these guidelines when:
  • Adding DBOS to existing Python code
  • Creating workflows and steps
  • Using queues for concurrency control
  • Implementing workflow communication (events, messages, streams)
  • Configuring and launching DBOS applications
  • Using DBOSClient from external applications
  • Testing DBOS applications
在以下场景中可参考本指南:
  • 为现有Python代码集成DBOS
  • 创建工作流与步骤
  • 使用队列进行并发控制
  • 实现工作流通信(事件、消息、流)
  • 配置与启动DBOS应用
  • 从外部应用调用DBOSClient
  • 测试DBOS应用

Rule Categories by Priority

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1LifecycleCRITICAL
lifecycle-
2WorkflowCRITICAL
workflow-
3StepHIGH
step-
4QueueHIGH
queue-
5CommunicationMEDIUM
comm-
6PatternMEDIUM
pattern-
7TestingLOW-MEDIUM
test-
8ClientMEDIUM
client-
9AdvancedLOW
advanced-
优先级类别影响程度前缀
1生命周期关键
lifecycle-
2工作流关键
workflow-
3步骤
step-
4队列
queue-
5通信
comm-
6模式
pattern-
7测试中低
test-
8客户端
client-
9进阶
advanced-

Critical Rules

核心规则

DBOS Configuration and Launch

DBOS配置与启动

A DBOS application MUST configure and launch DBOS inside its main function:
python
import os
from dbos import DBOS, DBOSConfig

@DBOS.workflow()
def my_workflow():
    pass

if __name__ == "__main__":
    config: DBOSConfig = {
        "name": "my-app",
        "system_database_url": os.environ.get("DBOS_SYSTEM_DATABASE_URL"),
    }
    DBOS(config=config)
    DBOS.launch()
DBOS应用必须在主函数内配置并启动DBOS:
python
import os
from dbos import DBOS, DBOSConfig

@DBOS.workflow()
def my_workflow():
    pass

if __name__ == "__main__":
    config: DBOSConfig = {
        "name": "my-app",
        "system_database_url": os.environ.get("DBOS_SYSTEM_DATABASE_URL"),
    }
    DBOS(config=config)
    DBOS.launch()

Workflow and Step Structure

工作流与步骤结构

Workflows are comprised of steps. Any function performing complex operations or accessing external services must be a step:
python
@DBOS.step()
def call_external_api():
    return requests.get("https://api.example.com").json()

@DBOS.workflow()
def my_workflow():
    result = call_external_api()
    return result
工作流由步骤组成。任何执行复杂操作或访问外部服务的函数都必须定义为步骤:
python
@DBOS.step()
def call_external_api():
    return requests.get("https://api.example.com").json()

@DBOS.workflow()
def my_workflow():
    result = call_external_api()
    return result

Key Constraints

关键约束

  • Do NOT call
    DBOS.start_workflow
    or
    DBOS.recv
    from a step
  • Do NOT use threads to start workflows - use
    DBOS.start_workflow
    or queues
  • Workflows MUST be deterministic - non-deterministic operations go in steps
  • Do NOT create/update global variables from workflows or steps
  • 不得在步骤内调用
    DBOS.start_workflow
    DBOS.recv
  • 不得使用线程启动工作流 - 请使用
    DBOS.start_workflow
    或队列
  • 工作流必须具备确定性 - 非确定性操作应放在步骤中
  • 不得从工作流或步骤中创建/更新全局变量

How to Use

使用方法

Read individual rule files for detailed explanations and examples:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md
请阅读单个规则文件获取详细说明与示例:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md

References

参考资料