异步执行#
所有 SkyPilot CLI 和 SDK 都是异步的。当调用 CLI 或 SDK 函数时,请求将被发送到 SkyPilot API server,并且其日志将流式传输回客户端。
任何请求都可以通过 Ctrl+C
安全地中断,请求将在 API server 上继续在后台运行。
SkyPilot API server#
SkyPilot 实现了客户端-服务器架构。在本地运行时,第一次 CLI 或 SDK 调用将自动在本地启动 SkyPilot API server,随后的任何 CLI 和 SDK 调用都将连接到同一个 API server。
管理员也可以为团队 部署远程 SkyPilot API server,以便多个用户可以共享同一个 API server。用户可以通过以下方式 连接到 API server:
$ sky api login
Enter the API server endpoint: http://1.2.3.4:30050
CLI#
例如,当用户运行 sky launch -c my-cluster
时,以下输出会流式传输到终端:
$ sky launch -c my-cluster --cpus 2
Considered resources (1 node):
---------------------------------------------------------------------------------------------
CLOUD INSTANCE vCPUs Mem(GB) ACCELERATORS REGION/ZONE COST ($) CHOSEN
---------------------------------------------------------------------------------------------
Kubernetes 2CPU--2GB 2 2 - in-cluster 0.00 ✔
AWS m6i.large 2 8 - us-east-1 0.10
---------------------------------------------------------------------------------------------
Launching a new cluster 'my-cluster'. Proceed? [Y/n]:
⚙︎ Launching on Kubernetes.
└── Pod is up.
⠴ Preparing SkyPilot runtime (2/3 - dependencies) View logs: sky api logs -l sky-2024-12-13-05-27-22-754475/provision.log
当用户使用 Ctrl+C
中断命令时,请求将在服务器上继续在后台运行。
用户可以使用 sky api logs
重新连接到请求的日志,或者使用 sky api cancel
取消请求。
$ sky launch -c my-cluster --cpus 2
...
^C
⚙︎ Request will continue running asynchronously.
├── View logs: sky api logs 73d316ac
├── Or, visit: http://127.0.0.1:46580/api/stream?request_id=73d316ac
└── To cancel the request, run: sky api cancel 73d316ac
作为特例,终止 (sky down my-cluster
) 或停止 (sky stop my-cluster
) 集群将自动取消该集群上所有现有请求,包括 PENDING
和 RUNNING
状态的请求。
注意
目前,sky jobs cancel
和 sky serve down
不会中止其他请求。
异步运行 CLI#
大多数 SkyPilot CLI 支持 --async
标志,该标志将异步提交请求并立即返回。使用此标志,您可以快速提交多个请求,而无需等待每个请求完成。
$ for i in {1..10}; do sky jobs launch -n job-$i -y --async "echo hello SkyPilot $i"; done
这对于启动多个并行作业特别有用。
Python SDK#
与 CLI 类似,SkyPilot SDK 调用会向 SkyPilot API server 发送异步请求。调用 SDK 函数时,它将返回一个请求 ID,可用于流式传输日志、等待请求完成或取消请求。
import sky
task = sky.Task(
run="echo hello SkyPilot", resources=sky.Resources(cloud=sky.AWS()))
# sky.launch() returns a request ID.
request_id = sky.launch(task, cluster_name="my-cluster")
# Stream logs and get the output.
job_id, handle = sky.stream_and_get(request_id)
# Tail the logs of the job. This is a synchronous call.
sky.tail_logs(job_id)
请注意,以下日志函数是同步的:
sky.tail_logs()
sky.download_logs()
sky.jobs.tail_logs()
sky.jobs.download_logs()
sky.serve.tail_logs()
注意
从 v0.8 或更旧版本升级: 如果您从等于或早于 0.8.0 的版本升级到任何更新版本,您使用 SkyPilot SDK 的程序需要更新以使用新的 sky.stream_and_get
函数来检索 SDK 函数调用的结果。更多详情请参阅迁移指南。
管理请求#
您可以通过 sky api
命令访问异步 SkyPilot 请求。
列出请求#
要查看服务器上的所有请求,运行 sky api status
。
$ # List all ongoing requests
$ sky api status
ID User Name Created Status
0d35ffa7-2813-4f3b-95c2-c5ab2238df50 user2 logs a few secs ago RUNNING
a9d59602-b82b-4cf8-a10f-5cde4dd76f29 user1 launch a few secs ago RUNNING
skypilot-status-refresh-daemon skypilot-system status 5 hrs ago RUNNING
$ # List all finished and ongoing requests
$ sky api status -a
提示
sky api status
显示每个请求的完整 ID,但您总可以在 sky api
命令中使用 ID 的前缀。
流式传输日志#
要流式传输请求的日志,运行 sky api logs <request-id>
。
$ sky api logs 0d35ffa7
取消请求#
要取消请求,运行 sky api cancel <request-id> <request-id> ...
。
$ sky api cancel 0d35ffa7 a9d59602