dbos-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDBOS 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
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Lifecycle | CRITICAL | |
| 2 | Workflow | CRITICAL | |
| 3 | Step | HIGH | |
| 4 | Queue | HIGH | |
| 5 | Communication | MEDIUM | |
| 6 | Pattern | MEDIUM | |
| 7 | Testing | LOW-MEDIUM | |
| 8 | Client | MEDIUM | |
| 9 | Advanced | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 生命周期 | 关键 | |
| 2 | 工作流 | 关键 | |
| 3 | 步骤 | 高 | |
| 4 | 队列 | 高 | |
| 5 | 通信 | 中 | |
| 6 | 模式 | 中 | |
| 7 | 测试 | 中低 | |
| 8 | 客户端 | 中 | |
| 9 | 进阶 | 低 | |
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 resultKey Constraints
关键约束
- Do NOT call or
DBOS.start_workflowfrom a stepDBOS.recv - Do NOT use threads to start workflows - use or queues
DBOS.start_workflow - Workflows MUST be deterministic - non-deterministic operations go in steps
- Do NOT create/update global variables from workflows or steps
- 不得在步骤内调用或
DBOS.start_workflowDBOS.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