You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e27c810
Browse filesBrowse the repository at this point in the historyBrowse files
1.Called from `worker.py:40` before heartbeat starts: `asyncio.run(run_fitness_checks())`
616
+
1.The first top-level `import runpod` with both `RUNPOD_ENDPOINT_ID` and `RUNPOD_WEBHOOK_GET_JOB` runs the built-in hardware checks (RAM, disk, CUDA version, native GPU test), excluding `RUNPOD_TEST`, `--test_input`, and `--rp_serve_api` invocations. On success the process sets `RUNPOD_EARLY_FITNESS_CHECKS_DONE=1`, which child processes inherit so they skip the pass. Network, Python CUDA initialization, compute, and custom checks remain at worker start. `RUNPOD_DEFER_FITNESS_CHECKS=true` postpones early checks. No custom launcher or entrypoint changes are required.
617
617
2. Runs only in production mode (skipped for local testing)
618
618
3. Auto-detects sync vs async using `inspect.iscoroutinefunction()`
619
619
4. Executes checks in registration order (list preserves order)
620
-
5. On failure: log detailed error, call `sys.exit(1)`
620
+
5. On health failure: log, best-effort unhealthy report, force-kill via `os._exit(1)`. Registration is atomic; early setup errors defer, unresolved worker-start setup errors report `fitness_check_setup` and force-exit.
621
621
6. On success: log completion, proceed with worker startup
622
622
623
623
**Performance**: ~0.5ms framework overhead per check, total depends on check logic
Copy file name to clipboardExpand all lines: README.md
+282Lines changed: 282 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,6 +104,288 @@ make setup
104
104
make test
105
105
```
106
106
107
+
## ⚡ | Serverless Worker (SDK)
108
+
109
+
This python package can also be used to create a serverless worker that can be deployed to Runpod as a custom endpoint API.
110
+
111
+
### Quick Start
112
+
113
+
Create a python script in your project that contains your model definition and the Runpod worker start code. Run this python code as your default container start command:
114
+
115
+
```python
116
+
# my_worker.py
117
+
118
+
import runpod
119
+
120
+
defis_even(job):
121
+
122
+
job_input = job["input"]
123
+
the_number = job_input["number"]
124
+
125
+
ifnotisinstance(the_number, int):
126
+
return {"error": "Silly human, you need to pass an integer."}
127
+
128
+
if the_number %2==0:
129
+
returnTrue
130
+
131
+
returnFalse
132
+
133
+
runpod.serverless.start({"handler": is_even})
134
+
```
135
+
136
+
Make sure that this file is ran when your container starts. This can be accomplished by calling it in the docker command when you set up a template at [console.runpod.io/serverless/user/templates](https://console.runpod.io/serverless/user/templates) or by setting it as the default command in your Dockerfile.
137
+
138
+
See our [blog post](https://www.runpod.io/blog/build-basic-serverless-api) for creating a basic Serverless API, or view the [details docs](https://docs.runpod.io/serverless-ai/custom-apis) for more information.
139
+
140
+
### Local Test Worker
141
+
142
+
You can also test your worker locally before deploying it to Runpod. This is useful for debugging and testing.
143
+
144
+
```bash
145
+
python my_worker.py --rp_serve_api
146
+
```
147
+
148
+
### Worker Fitness Checks
149
+
150
+
Fitness checks allow you to validate your worker environment at startup before processing jobs. If any check fails, the worker exits immediately, allowing your orchestrator to restart it.
151
+
152
+
```python
153
+
# my_worker.py
154
+
155
+
import runpod
156
+
import torch
157
+
158
+
# Register fitness checks using the decorator
159
+
@runpod.serverless.register_fitness_check
160
+
defcheck_gpu_available():
161
+
"""Verify GPU is available."""
162
+
ifnot torch.cuda.is_available():
163
+
raiseRuntimeError("GPU not available")
164
+
165
+
@runpod.serverless.register_fitness_check
166
+
defcheck_disk_space():
167
+
"""Verify sufficient disk space."""
168
+
import shutil
169
+
stat = shutil.disk_usage("/")
170
+
free_gb = stat.free / (1024**3)
171
+
if free_gb <10:
172
+
raiseRuntimeError(f"Insufficient disk space: {free_gb:.2f}GB free")
173
+
174
+
defhandler(job):
175
+
job_input = job["input"]
176
+
# Your handler code here
177
+
return {"output": "success"}
178
+
179
+
# Fitness checks run before handler initialization (production only)
180
+
runpod.serverless.start({"handler": handler})
181
+
```
182
+
183
+
**Key Features:**
184
+
- Supports both synchronous and asynchronous check functions
185
+
- Hardware checks run at the first Serverless `import runpod`; processes it launches inherit the result via `RUNPOD_EARLY_FITNESS_CHECKS_DONE` and skip the pass
186
+
- Local tests and non-worker imports remain exempt; network readiness and custom checks run at worker start
187
+
- Successful early checks are reused at worker start unless their configuration changes
188
+
- Runs before handler initialization and job processing begins
189
+
- Any check failure exits with code 1 (worker marked unhealthy)
190
+
191
+
See [Worker Fitness Checks](https://github.com/runpod/runpod-python/blob/main/docs/serverless/worker_fitness_checks.md) documentation for more examples and best practices.
192
+
193
+
### Network-Volume Warm Cache
194
+
195
+
When a network volume is attached, `VolumeCache` warms local directories (such as a model cache) across cold starts — hydrating them on startup and syncing new files back on exit — so a repeated multi-GB model download becomes a one-time cost per endpoint. It is stdlib-only and best-effort.
196
+
197
+
```python
198
+
from runpod.serverless import VolumeCache
199
+
200
+
with VolumeCache(dirs=["/root/.cache/huggingface"]):
201
+
model = load_model()
202
+
```
203
+
204
+
See [Network-Volume Warm Cache](https://github.com/runpod/runpod-python/blob/main/docs/serverless/volume_cache.md) documentation for configuration and details.
205
+
206
+
## 📚 | REST API v2 Wrapper
207
+
208
+
Use the API wrapper to manage Runpod resources through REST API v2.
0 commit comments