scalebox-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scalebox/__init__.py +80 -0
- scalebox/api/__init__.py +128 -0
- scalebox/api/client/__init__.py +8 -0
- scalebox/api/client/api/__init__.py +1 -0
- scalebox/api/client/api/sandboxes/__init__.py +0 -0
- scalebox/api/client/api/sandboxes/delete_sandboxes_sandbox_id.py +161 -0
- scalebox/api/client/api/sandboxes/get_sandboxes.py +176 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_metrics.py +173 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id.py +163 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_logs.py +199 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +214 -0
- scalebox/api/client/api/sandboxes/get_v2_sandboxes.py +229 -0
- scalebox/api/client/api/sandboxes/post_sandboxes.py +174 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_pause.py +165 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_refreshes.py +182 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_resume.py +190 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_timeout.py +194 -0
- scalebox/api/client/client.py +288 -0
- scalebox/api/client/errors.py +16 -0
- scalebox/api/client/models/__init__.py +81 -0
- scalebox/api/client/models/build_log_entry.py +79 -0
- scalebox/api/client/models/created_access_token.py +100 -0
- scalebox/api/client/models/created_team_api_key.py +166 -0
- scalebox/api/client/models/error.py +67 -0
- scalebox/api/client/models/identifier_masking_details.py +83 -0
- scalebox/api/client/models/listed_sandbox.py +138 -0
- scalebox/api/client/models/log_level.py +11 -0
- scalebox/api/client/models/new_access_token.py +59 -0
- scalebox/api/client/models/new_sandbox.py +125 -0
- scalebox/api/client/models/new_team_api_key.py +59 -0
- scalebox/api/client/models/node.py +154 -0
- scalebox/api/client/models/node_detail.py +152 -0
- scalebox/api/client/models/node_status.py +11 -0
- scalebox/api/client/models/node_status_change.py +61 -0
- scalebox/api/client/models/post_sandboxes_sandbox_id_refreshes_body.py +59 -0
- scalebox/api/client/models/post_sandboxes_sandbox_id_timeout_body.py +59 -0
- scalebox/api/client/models/resumed_sandbox.py +68 -0
- scalebox/api/client/models/sandbox.py +125 -0
- scalebox/api/client/models/sandbox_detail.py +178 -0
- scalebox/api/client/models/sandbox_log.py +70 -0
- scalebox/api/client/models/sandbox_logs.py +73 -0
- scalebox/api/client/models/sandbox_metric.py +110 -0
- scalebox/api/client/models/sandbox_state.py +9 -0
- scalebox/api/client/models/sandboxes_with_metrics.py +59 -0
- scalebox/api/client/models/team.py +83 -0
- scalebox/api/client/models/team_api_key.py +158 -0
- scalebox/api/client/models/team_user.py +68 -0
- scalebox/api/client/models/template.py +179 -0
- scalebox/api/client/models/template_build.py +117 -0
- scalebox/api/client/models/template_build_file_upload.py +70 -0
- scalebox/api/client/models/template_build_request.py +115 -0
- scalebox/api/client/models/template_build_request_v2.py +88 -0
- scalebox/api/client/models/template_build_start_v2.py +114 -0
- scalebox/api/client/models/template_build_status.py +11 -0
- scalebox/api/client/models/template_step.py +91 -0
- scalebox/api/client/models/template_update_request.py +59 -0
- scalebox/api/client/models/update_team_api_key.py +59 -0
- scalebox/api/client/py.typed +1 -0
- scalebox/api/client/types.py +46 -0
- scalebox/api/metadata.py +19 -0
- scalebox/cli.py +125 -0
- scalebox/client/__init__.py +0 -0
- scalebox/client/aclient.py +57 -0
- scalebox/client/api.proto +460 -0
- scalebox/client/buf.gen.yaml +8 -0
- scalebox/client/client.py +102 -0
- scalebox/client/requirements.txt +5 -0
- scalebox/code_interpreter/__init__.py +12 -0
- scalebox/code_interpreter/charts.py +230 -0
- scalebox/code_interpreter/code_interpreter_async.py +369 -0
- scalebox/code_interpreter/code_interpreter_sync.py +317 -0
- scalebox/code_interpreter/constants.py +3 -0
- scalebox/code_interpreter/exceptions.py +13 -0
- scalebox/code_interpreter/models.py +485 -0
- scalebox/connection_config.py +92 -0
- scalebox/csx_connect/__init__.py +1 -0
- scalebox/csx_connect/client.py +485 -0
- scalebox/csx_desktop/__init__.py +0 -0
- scalebox/csx_desktop/main.py +651 -0
- scalebox/exceptions.py +83 -0
- scalebox/generated/__init__.py +0 -0
- scalebox/generated/api.py +61 -0
- scalebox/generated/api_pb2.py +203 -0
- scalebox/generated/api_pb2.pyi +956 -0
- scalebox/generated/api_pb2_connect.py +1456 -0
- scalebox/generated/rpc.py +50 -0
- scalebox/generated/versions.py +3 -0
- scalebox/requirements.txt +36 -0
- scalebox/sandbox/__init__.py +0 -0
- scalebox/sandbox/commands/__init__.py +0 -0
- scalebox/sandbox/commands/command_handle.py +69 -0
- scalebox/sandbox/commands/main.py +39 -0
- scalebox/sandbox/filesystem/__init__.py +0 -0
- scalebox/sandbox/filesystem/filesystem.py +95 -0
- scalebox/sandbox/filesystem/watch_handle.py +60 -0
- scalebox/sandbox/main.py +139 -0
- scalebox/sandbox/sandbox_api.py +91 -0
- scalebox/sandbox/signature.py +40 -0
- scalebox/sandbox/utils.py +34 -0
- scalebox/sandbox_async/__init__.py +1 -0
- scalebox/sandbox_async/commands/command.py +307 -0
- scalebox/sandbox_async/commands/command_handle.py +187 -0
- scalebox/sandbox_async/commands/pty.py +187 -0
- scalebox/sandbox_async/filesystem/filesystem.py +557 -0
- scalebox/sandbox_async/filesystem/watch_handle.py +61 -0
- scalebox/sandbox_async/main.py +646 -0
- scalebox/sandbox_async/sandbox_api.py +365 -0
- scalebox/sandbox_async/utils.py +7 -0
- scalebox/sandbox_sync/__init__.py +2 -0
- scalebox/sandbox_sync/commands/__init__.py +0 -0
- scalebox/sandbox_sync/commands/command.py +300 -0
- scalebox/sandbox_sync/commands/command_handle.py +150 -0
- scalebox/sandbox_sync/commands/pty.py +181 -0
- scalebox/sandbox_sync/filesystem/__init__.py +0 -0
- scalebox/sandbox_sync/filesystem/filesystem.py +543 -0
- scalebox/sandbox_sync/filesystem/watch_handle.py +66 -0
- scalebox/sandbox_sync/main.py +790 -0
- scalebox/sandbox_sync/sandbox_api.py +356 -0
- scalebox/test/CODE_INTERPRETER_TESTS_READY.md +323 -0
- scalebox/test/README.md +329 -0
- scalebox/test/__init__.py +0 -0
- scalebox/test/aclient.py +72 -0
- scalebox/test/code_interpreter_centext.py +21 -0
- scalebox/test/code_interpreter_centext_sync.py +21 -0
- scalebox/test/code_interpreter_test.py +34 -0
- scalebox/test/code_interpreter_test_sync.py +34 -0
- scalebox/test/run_all_validation_tests.py +334 -0
- scalebox/test/run_code_interpreter_tests.sh +67 -0
- scalebox/test/run_tests.sh +230 -0
- scalebox/test/test_basic.py +78 -0
- scalebox/test/test_code_interpreter_async_comprehensive.py +2653 -0
- scalebox/test/test_code_interpreter_e2basync_comprehensive.py +2655 -0
- scalebox/test/test_code_interpreter_e2bsync_comprehensive.py +3416 -0
- scalebox/test/test_code_interpreter_sync_comprehensive.py +3412 -0
- scalebox/test/test_e2b_first.py +11 -0
- scalebox/test/test_sandbox_async_comprehensive.py +738 -0
- scalebox/test/test_sandbox_stress_and_edge_cases.py +778 -0
- scalebox/test/test_sandbox_sync_comprehensive.py +770 -0
- scalebox/test/test_sandbox_usage_examples.py +987 -0
- scalebox/test/testacreate.py +24 -0
- scalebox/test/testagetinfo.py +18 -0
- scalebox/test/testcodeinterpreter_async.py +508 -0
- scalebox/test/testcodeinterpreter_sync.py +239 -0
- scalebox/test/testcomputeuse.py +243 -0
- scalebox/test/testnovnc.py +12 -0
- scalebox/test/testsandbox_async.py +118 -0
- scalebox/test/testsandbox_sync.py +38 -0
- scalebox/utils/__init__.py +0 -0
- scalebox/utils/httpcoreclient.py +297 -0
- scalebox/utils/httpxclient.py +403 -0
- scalebox/version.py +16 -0
- scalebox_sdk-0.1.0.dist-info/METADATA +292 -0
- scalebox_sdk-0.1.0.dist-info/RECORD +157 -0
- scalebox_sdk-0.1.0.dist-info/WHEEL +5 -0
- scalebox_sdk-0.1.0.dist-info/entry_points.txt +2 -0
- scalebox_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
- scalebox_sdk-0.1.0.dist-info/top_level.txt +1 -0
scalebox/test/README.md
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# ScaleBox Sandbox 验证测试套件
|
|
2
|
+
|
|
3
|
+
这个测试套件包含了针对 `sandbox_async` 和 `sandbox_sync` 模块的全面验证示例,涵盖了从基本功能到高级使用场景的各个方面。
|
|
4
|
+
|
|
5
|
+
## 📋 测试文件概览
|
|
6
|
+
|
|
7
|
+
### 1. 基础功能验证
|
|
8
|
+
|
|
9
|
+
#### `test_sandbox_async_comprehensive.py`
|
|
10
|
+
**异步沙箱综合验证测试**
|
|
11
|
+
- ✅ 沙箱生命周期管理(创建、连接、销毁)
|
|
12
|
+
- ✅ 文件系统操作(读写、列表、删除、重命名等)
|
|
13
|
+
- ✅ 命令执行(前台、后台、PTY)
|
|
14
|
+
- ✅ 静态方法和类方法
|
|
15
|
+
- ✅ 错误处理和异常情况
|
|
16
|
+
- ✅ 性能测试和并发操作
|
|
17
|
+
- ✅ 上下文管理器使用
|
|
18
|
+
|
|
19
|
+
**运行方式:**
|
|
20
|
+
```bash
|
|
21
|
+
cd /home/ubuntu/git_home/scalebox/test
|
|
22
|
+
python test_sandbox_async_comprehensive.py
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
#### `test_sandbox_sync_comprehensive.py`
|
|
26
|
+
**同步沙箱综合验证测试**
|
|
27
|
+
- ✅ 与异步版本相同的功能覆盖
|
|
28
|
+
- ✅ 同步特定的操作模式
|
|
29
|
+
- ✅ 线程池并发处理
|
|
30
|
+
- ✅ 目录监控功能
|
|
31
|
+
|
|
32
|
+
**运行方式:**
|
|
33
|
+
```bash
|
|
34
|
+
cd /home/ubuntu/git_home/scalebox/test
|
|
35
|
+
python test_sandbox_sync_comprehensive.py
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. 压力测试和边界条件
|
|
39
|
+
|
|
40
|
+
#### `test_sandbox_stress_and_edge_cases.py`
|
|
41
|
+
**压力测试和边界条件验证**
|
|
42
|
+
- 🔥 大数据文件处理(10MB+ 文件)
|
|
43
|
+
- 🔥 高并发操作(同步和异步)
|
|
44
|
+
- 🔥 内存压力测试(1000+ 文件操作)
|
|
45
|
+
- 🔥 边界条件测试(空文件、特殊字符、深层目录)
|
|
46
|
+
- 🔥 错误恢复能力测试
|
|
47
|
+
- 🔥 资源管理测试(多沙箱实例)
|
|
48
|
+
|
|
49
|
+
**运行方式:**
|
|
50
|
+
```bash
|
|
51
|
+
cd /home/ubuntu/git_home/scalebox/test
|
|
52
|
+
python test_sandbox_stress_and_edge_cases.py
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 3. 实际应用示例
|
|
56
|
+
|
|
57
|
+
#### `test_sandbox_usage_examples.py`
|
|
58
|
+
**使用示例和最佳实践**
|
|
59
|
+
- 💡 代码执行服务实现
|
|
60
|
+
- 💡 文件处理服务实现
|
|
61
|
+
- 💡 交互式会话管理
|
|
62
|
+
- 💡 性能优化技巧
|
|
63
|
+
- 💡 资源管理最佳实践
|
|
64
|
+
- 💡 错误处理策略
|
|
65
|
+
- 💡 并发和异步编程模式
|
|
66
|
+
|
|
67
|
+
**运行方式:**
|
|
68
|
+
```bash
|
|
69
|
+
cd /home/ubuntu/git_home/scalebox/test
|
|
70
|
+
python test_sandbox_usage_examples.py
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 🏗️ 测试架构设计
|
|
74
|
+
|
|
75
|
+
### 测试结果记录系统
|
|
76
|
+
每个测试套件都包含完整的测试结果记录系统:
|
|
77
|
+
```python
|
|
78
|
+
class TestValidator:
|
|
79
|
+
def log_test_result(self, test_name: str, success: bool, message: str = "", duration: float = 0)
|
|
80
|
+
def run_test(self, test_func, test_name: str)
|
|
81
|
+
def print_summary(self)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 资源管理系统
|
|
85
|
+
智能的沙箱资源管理:
|
|
86
|
+
```python
|
|
87
|
+
class SandboxManager:
|
|
88
|
+
@contextmanager
|
|
89
|
+
def get_sandbox(self, sandbox_id: Optional[str] = None)
|
|
90
|
+
|
|
91
|
+
class AsyncSandboxManager:
|
|
92
|
+
@asynccontextmanager
|
|
93
|
+
async def get_sandbox(self, sandbox_id: Optional[str] = None)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 重试机制
|
|
97
|
+
内置的重试装饰器:
|
|
98
|
+
```python
|
|
99
|
+
@retry_on_failure(max_retries=3, delay=1.0)
|
|
100
|
+
def your_function():
|
|
101
|
+
# 自动重试失败的操作
|
|
102
|
+
pass
|
|
103
|
+
|
|
104
|
+
@async_retry_on_failure(max_retries=3, delay=1.0)
|
|
105
|
+
async def your_async_function():
|
|
106
|
+
# 异步重试机制
|
|
107
|
+
pass
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 📊 测试覆盖范围
|
|
111
|
+
|
|
112
|
+
### 功能覆盖
|
|
113
|
+
| 功能模块 | 同步测试 | 异步测试 | 压力测试 | 使用示例 |
|
|
114
|
+
|---------|---------|---------|---------|---------|
|
|
115
|
+
| 沙箱创建/销毁 | ✅ | ✅ | ✅ | ✅ |
|
|
116
|
+
| 文件系统操作 | ✅ | ✅ | ✅ | ✅ |
|
|
117
|
+
| 命令执行 | ✅ | ✅ | ✅ | ✅ |
|
|
118
|
+
| PTY 操作 | ✅ | ✅ | ✅ | ✅ |
|
|
119
|
+
| 静态方法 | ✅ | ✅ | ✅ | ✅ |
|
|
120
|
+
| 错误处理 | ✅ | ✅ | ✅ | ✅ |
|
|
121
|
+
| 性能优化 | ✅ | ✅ | ✅ | ✅ |
|
|
122
|
+
| 并发处理 | ✅ | ✅ | ✅ | ✅ |
|
|
123
|
+
|
|
124
|
+
### 测试场景
|
|
125
|
+
- **基础功能**:单一操作的正确性验证
|
|
126
|
+
- **批量操作**:大量数据的批处理能力
|
|
127
|
+
- **并发处理**:多任务并行执行
|
|
128
|
+
- **错误恢复**:异常情况下的系统稳定性
|
|
129
|
+
- **资源管理**:内存和连接的有效管理
|
|
130
|
+
- **性能基准**:各种操作的性能指标
|
|
131
|
+
|
|
132
|
+
## 🚀 快速开始
|
|
133
|
+
|
|
134
|
+
### 1. 环境准备
|
|
135
|
+
确保你的环境中已经安装了必要的依赖:
|
|
136
|
+
```bash
|
|
137
|
+
# 确保模块路径正确
|
|
138
|
+
export PYTHONPATH=/home/ubuntu/git_home/scalebox:$PYTHONPATH
|
|
139
|
+
|
|
140
|
+
# 安装依赖(如果需要)
|
|
141
|
+
pip install -r requirements.txt
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2. 运行单个测试
|
|
145
|
+
```bash
|
|
146
|
+
# 运行异步综合测试
|
|
147
|
+
python test_sandbox_async_comprehensive.py
|
|
148
|
+
|
|
149
|
+
# 运行同步综合测试
|
|
150
|
+
python test_sandbox_sync_comprehensive.py
|
|
151
|
+
|
|
152
|
+
# 运行压力测试
|
|
153
|
+
python test_sandbox_stress_and_edge_cases.py
|
|
154
|
+
|
|
155
|
+
# 运行使用示例
|
|
156
|
+
python test_sandbox_usage_examples.py
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 3. 批量运行所有测试
|
|
160
|
+
```bash
|
|
161
|
+
# 创建运行脚本
|
|
162
|
+
cat > run_all_tests.sh << 'EOF'
|
|
163
|
+
#!/bin/bash
|
|
164
|
+
|
|
165
|
+
echo "开始运行所有沙箱测试..."
|
|
166
|
+
|
|
167
|
+
echo "=== 异步综合测试 ==="
|
|
168
|
+
python test_sandbox_async_comprehensive.py
|
|
169
|
+
|
|
170
|
+
echo "=== 同步综合测试 ==="
|
|
171
|
+
python test_sandbox_sync_comprehensive.py
|
|
172
|
+
|
|
173
|
+
echo "=== 压力测试和边界条件 ==="
|
|
174
|
+
python test_sandbox_stress_and_edge_cases.py
|
|
175
|
+
|
|
176
|
+
echo "=== 使用示例和最佳实践 ==="
|
|
177
|
+
python test_sandbox_usage_examples.py
|
|
178
|
+
|
|
179
|
+
echo "所有测试完成!"
|
|
180
|
+
EOF
|
|
181
|
+
|
|
182
|
+
chmod +x run_all_tests.sh
|
|
183
|
+
./run_all_tests.sh
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 📈 性能基准
|
|
187
|
+
|
|
188
|
+
### 同步 vs 异步性能对比
|
|
189
|
+
|
|
190
|
+
| 操作类型 | 同步版本 | 异步版本 | 性能提升 |
|
|
191
|
+
|---------|---------|---------|---------|
|
|
192
|
+
| 单文件操作 | ~0.1s | ~0.1s | 持平 |
|
|
193
|
+
| 批量文件操作(100个) | ~2.0s | ~0.5s | **4x 更快** |
|
|
194
|
+
| 并发命令执行(10个) | ~1.5s | ~0.3s | **5x 更快** |
|
|
195
|
+
| PTY 交互会话 | ~0.2s | ~0.2s | 持平 |
|
|
196
|
+
|
|
197
|
+
### 资源使用情况
|
|
198
|
+
- **内存使用**:异步版本在大批量操作时内存使用更高效
|
|
199
|
+
- **CPU 利用率**:异步版本能更好地利用多核 CPU
|
|
200
|
+
- **网络连接**:异步版本支持更多并发连接
|
|
201
|
+
|
|
202
|
+
## 🛠️ 自定义测试
|
|
203
|
+
|
|
204
|
+
### 添加新测试
|
|
205
|
+
1. **继承测试基类**:
|
|
206
|
+
```python
|
|
207
|
+
class YourTestValidator:
|
|
208
|
+
def __init__(self):
|
|
209
|
+
self.test_results = []
|
|
210
|
+
self.failed_tests = []
|
|
211
|
+
|
|
212
|
+
def log_test_result(self, test_name: str, success: bool, message: str = "", duration: float = 0):
|
|
213
|
+
# 实现测试结果记录
|
|
214
|
+
pass
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
2. **编写测试方法**:
|
|
218
|
+
```python
|
|
219
|
+
def test_your_feature(self):
|
|
220
|
+
"""测试你的功能"""
|
|
221
|
+
# 测试逻辑
|
|
222
|
+
pass
|
|
223
|
+
|
|
224
|
+
async def test_your_async_feature(self):
|
|
225
|
+
"""测试你的异步功能"""
|
|
226
|
+
# 异步测试逻辑
|
|
227
|
+
pass
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
3. **运行测试**:
|
|
231
|
+
```python
|
|
232
|
+
def run_all_tests(self):
|
|
233
|
+
self.run_test(self.test_your_feature, "Your Feature Test")
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### 配置测试参数
|
|
237
|
+
```python
|
|
238
|
+
# 在测试文件顶部配置
|
|
239
|
+
TEST_CONFIG = {
|
|
240
|
+
'debug_mode': True,
|
|
241
|
+
'max_concurrent': 10,
|
|
242
|
+
'timeout': 30,
|
|
243
|
+
'large_file_size': 10 * 1024 * 1024, # 10MB
|
|
244
|
+
'stress_test_iterations': 1000
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## 🐛 故障排除
|
|
249
|
+
|
|
250
|
+
### 常见问题
|
|
251
|
+
|
|
252
|
+
1. **沙箱创建失败**
|
|
253
|
+
- 检查网络连接
|
|
254
|
+
- 验证 API 密钥配置
|
|
255
|
+
- 确认服务器状态
|
|
256
|
+
|
|
257
|
+
2. **文件操作失败**
|
|
258
|
+
- 检查文件路径权限
|
|
259
|
+
- 验证磁盘空间
|
|
260
|
+
- 确认文件格式
|
|
261
|
+
|
|
262
|
+
3. **命令执行超时**
|
|
263
|
+
- 调整超时参数
|
|
264
|
+
- 检查命令语法
|
|
265
|
+
- 验证环境变量
|
|
266
|
+
|
|
267
|
+
4. **内存不足错误**
|
|
268
|
+
- 减少并发数量
|
|
269
|
+
- 调整批处理大小
|
|
270
|
+
- 增加系统内存
|
|
271
|
+
|
|
272
|
+
### 调试技巧
|
|
273
|
+
|
|
274
|
+
1. **启用调试日志**:
|
|
275
|
+
```python
|
|
276
|
+
import logging
|
|
277
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
2. **使用调试模式**:
|
|
281
|
+
```python
|
|
282
|
+
sandbox = Sandbox(debug=True)
|
|
283
|
+
# 或
|
|
284
|
+
sandbox = await AsyncSandbox.create(debug=True)
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
3. **监控资源使用**:
|
|
288
|
+
```python
|
|
289
|
+
import psutil
|
|
290
|
+
print(f"内存使用: {psutil.virtual_memory().percent}%")
|
|
291
|
+
print(f"CPU 使用: {psutil.cpu_percent()}%")
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## 📝 贡献指南
|
|
295
|
+
|
|
296
|
+
### 添加新测试
|
|
297
|
+
1. Fork 项目
|
|
298
|
+
2. 创建特性分支
|
|
299
|
+
3. 添加测试用例
|
|
300
|
+
4. 更新文档
|
|
301
|
+
5. 提交 Pull Request
|
|
302
|
+
|
|
303
|
+
### 测试规范
|
|
304
|
+
- 每个测试函数都应该有清晰的文档字符串
|
|
305
|
+
- 使用断言验证结果
|
|
306
|
+
- 包含适当的错误处理
|
|
307
|
+
- 记录性能指标
|
|
308
|
+
- 清理测试资源
|
|
309
|
+
|
|
310
|
+
## 📚 参考资料
|
|
311
|
+
|
|
312
|
+
### API 文档
|
|
313
|
+
- [AsyncSandbox API](../sandbox_async/main.py)
|
|
314
|
+
- [Sandbox API](../sandbox_sync/main.py)
|
|
315
|
+
- [文件系统 API](../sandbox/filesystem/)
|
|
316
|
+
- [命令执行 API](../sandbox/commands/)
|
|
317
|
+
|
|
318
|
+
### 最佳实践
|
|
319
|
+
- 始终使用上下文管理器管理沙箱资源
|
|
320
|
+
- 批量操作优于单个操作
|
|
321
|
+
- 异步版本适合 I/O 密集型任务
|
|
322
|
+
- 同步版本适合 CPU 密集型任务
|
|
323
|
+
- 实现适当的重试和错误恢复机制
|
|
324
|
+
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
**Happy Testing! 🎉**
|
|
328
|
+
|
|
329
|
+
如有问题或建议,请提交 Issue 或联系维护团队。
|
|
File without changes
|
scalebox/test/aclient.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# import sys, os
|
|
2
|
+
# sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'generated'))
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
import aiohttp
|
|
6
|
+
from generated import api_pb2
|
|
7
|
+
from generated.api_pb2_connect import AsyncFilesystemClient
|
|
8
|
+
from tenacity import (
|
|
9
|
+
retry,
|
|
10
|
+
retry_if_exception_type,
|
|
11
|
+
stop_after_attempt,
|
|
12
|
+
wait_exponential,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@retry(
|
|
17
|
+
stop=stop_after_attempt(5), # 最大重试5次
|
|
18
|
+
wait=wait_exponential(multiplier=1, min=4, max=10), # 指数退避等待
|
|
19
|
+
retry=retry_if_exception_type(
|
|
20
|
+
(aiohttp.ClientError, asyncio.TimeoutError)
|
|
21
|
+
), # 针对这些异常重试
|
|
22
|
+
)
|
|
23
|
+
async def watch_directory_example():
|
|
24
|
+
# 创建 aiohttp 客户端会话
|
|
25
|
+
async with aiohttp.ClientSession(
|
|
26
|
+
timeout=aiohttp.ClientTimeout(total=None)
|
|
27
|
+
) as session:
|
|
28
|
+
# 创建文件系统客户端
|
|
29
|
+
client = AsyncFilesystemClient(
|
|
30
|
+
base_url="http://localhost:8080",
|
|
31
|
+
http_client=session,
|
|
32
|
+
# protocol=ConnectProtocol.CONNECT_PROTOBUF # 如果需要指定协议
|
|
33
|
+
)
|
|
34
|
+
# extra = {"authorization": "Bearer root"}
|
|
35
|
+
# 创建监视请求
|
|
36
|
+
request = api_pb2.WatchDirRequest(path="/root")
|
|
37
|
+
|
|
38
|
+
# 可选:添加额外的请求头
|
|
39
|
+
extra_headers = {
|
|
40
|
+
"Authorization": "Bearer root",
|
|
41
|
+
"X-Custom-Header": "custom-value",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
# 使用 async for 循环处理流式响应
|
|
46
|
+
async for response in client.watch_dir(
|
|
47
|
+
request, extra_headers=extra_headers
|
|
48
|
+
):
|
|
49
|
+
print(f"Received event: {response}")
|
|
50
|
+
# 在这里处理每个事件
|
|
51
|
+
# 例如,根据事件类型执行不同的操作
|
|
52
|
+
|
|
53
|
+
# 如果收到特定事件,可以中断循环
|
|
54
|
+
# if response.event_type == api_pb2.WatchDirResponse.EVENT_TYPE_STOP:
|
|
55
|
+
# break
|
|
56
|
+
|
|
57
|
+
except Exception as e:
|
|
58
|
+
print(f"Error during directory watching: {e}")
|
|
59
|
+
|
|
60
|
+
finally:
|
|
61
|
+
# 关闭会话(在 with 语句中会自动关闭,但这里为了清晰展示)
|
|
62
|
+
await session.close()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def main():
|
|
66
|
+
# 运行监视示例
|
|
67
|
+
await watch_directory_example()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__":
|
|
71
|
+
# 运行异步主函数
|
|
72
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
from scalebox.code_interpreter import AsyncSandbox
|
|
5
|
+
|
|
6
|
+
# from scalebox.sandbox_async.main import AsyncSandbox
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def pty_output_handler(output):
|
|
10
|
+
"""处理 PTY 输出的回调函数"""
|
|
11
|
+
print(f"输出: {output}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def main():
|
|
15
|
+
sandbox = AsyncSandbox()
|
|
16
|
+
context = await sandbox.create_code_context(language="python3")
|
|
17
|
+
print(context.__dict__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == "__main__":
|
|
21
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
from scalebox.code_interpreter import Sandbox
|
|
5
|
+
|
|
6
|
+
# from scalebox.sandbox_async.main import AsyncSandbox
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def pty_output_handler(output):
|
|
10
|
+
"""处理 PTY 输出的回调函数"""
|
|
11
|
+
print(f"输出: {output}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
sandbox = Sandbox()
|
|
16
|
+
context = sandbox.create_code_context(language="python3")
|
|
17
|
+
print(context.__dict__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == "__main__":
|
|
21
|
+
main()
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
from scalebox.code_interpreter import AsyncSandbox
|
|
5
|
+
|
|
6
|
+
# from scalebox.sandbox_async.main import AsyncSandbox
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def pty_output_handler(output):
|
|
10
|
+
"""处理 PTY 输出的回调函数"""
|
|
11
|
+
print(f"输出: {output}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def main():
|
|
15
|
+
sandbox = AsyncSandbox.create()
|
|
16
|
+
proc = await sandbox.run_code(
|
|
17
|
+
"""
|
|
18
|
+
import time
|
|
19
|
+
for i in range(3):
|
|
20
|
+
print("Hello E2B", i)
|
|
21
|
+
time.sleep(50)
|
|
22
|
+
""",
|
|
23
|
+
language="python3",
|
|
24
|
+
request_timeout=3600,
|
|
25
|
+
on_stdout=pty_output_handler,
|
|
26
|
+
on_stderr=pty_output_handler,
|
|
27
|
+
on_result=pty_output_handler,
|
|
28
|
+
)
|
|
29
|
+
print(proc)
|
|
30
|
+
time.sleep(10)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
from code_interpreter import Sandbox
|
|
5
|
+
|
|
6
|
+
# from scalebox.sandbox_async.main import AsyncSandbox
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def pty_output_handler(output):
|
|
10
|
+
"""处理 PTY 输出的回调函数"""
|
|
11
|
+
print(f"输出: {output}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
sandbox = Sandbox()
|
|
16
|
+
proc = sandbox.run_code(
|
|
17
|
+
"""
|
|
18
|
+
import time
|
|
19
|
+
for i in range(3):
|
|
20
|
+
print("Hello E2B", i)
|
|
21
|
+
time.sleep(50)
|
|
22
|
+
""",
|
|
23
|
+
language="python3",
|
|
24
|
+
request_timeout=3600,
|
|
25
|
+
on_stdout=pty_output_handler,
|
|
26
|
+
on_stderr=pty_output_handler,
|
|
27
|
+
on_result=pty_output_handler,
|
|
28
|
+
)
|
|
29
|
+
print(proc)
|
|
30
|
+
time.sleep(10)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
main()
|