[ PROMPT_NODE_26832 ]
scaling
[ SKILL_DOCUMENTATION ]
# 在 Modal 上进行扩展
## 自动伸缩
每个 Modal 函数都对应一个容器自动伸缩池。Modal 的自动伸缩器:
- 当没有可用容量时启动容器
- 当资源空闲时关闭容器
- 默认情况下,在没有输入需要处理时缩减为零
自动伸缩决策执行得既快速又频繁。
## 使用 `.map()` 进行并行执行
使用不同的输入并行重复运行函数:
python
@app.function()
def evaluate_model(x):
return x ** 2
@app.local_entrypoint()
def main():
inputs = list(range(100))
# 在容器间并行运行 100 个输入
for result in evaluate_model.map(inputs):
print(result)
### 使用 `.starmap()` 处理多个参数
对于具有多个参数的函数:
python
@app.function()
def add(a, b):
return a + b
@app.local_entrypoint()
def main():
results = list(add.starmap([(1, 2), (3, 4)]))
# [3, 7]
### 异常处理
python
@app.function()
def may_fail(a):
if a == 2:
raise Exception("error")
return a ** 2
@app.local_entrypoint()
def main():
results = list(may_fail.map(
range(3),
return_exceptions=True,
wrap_returned_exceptions=False
))
# [0, 1, Exception('error')]
## 自动伸缩配置
使用参数配置自动伸缩器的行为:
python
@app.function(
max_containers=100, # 容器上限
min_containers=2, # 即使不活动也保持预热
buffer_containers=5, # 活动时保持缓冲区
scaledown_window=60, # 缩减前的最大空闲时间(秒)
)
def my_function():
...
参数:
- **max_containers**: 容器总数上限
- **min_containers**: 即使不活动也保持预热的最小数量
- **buffer_containers**: 函数活动时的缓冲区大小(额外的输入无需排队)
- **scaledown_window**: 缩减前的最大空闲持续时间(秒)
权衡:
- 更大的预热池/缓冲区 → 更高的成本,更低的延迟
- 更长的缩减窗口 → 减少不频繁请求的抖动
## 动态更新自动伸缩器
无需重新部署即可更新自动伸缩器设置:
python
f = modal.Function.from_name("my-app", "f")
f.update_autoscaler(max_containers=100)
设置会在下次部署时恢复为装饰器配置,或被后续更新覆盖:
python
f.update_autoscaler(min_containers=2, max_containers=10)
f.update_autoscaler(min_containers=4) # max_containe