Diffinery

homeflare/celldpublic

celld/docs/services/workers.md
149 lines · 6.7 KBRaw History
1# Workers
2
3A Worker is a stateless request handler that uses the
4[Cloudflare Workers API](https://developers.cloudflare.com/workers/runtime-apis/).
5celld runs the deployment in a V8 isolate on every node of the fleet.
6
7## Example
8
9The [hello example](../../examples/hello) returns a text response from a
10`fetch` handler.
11
12<!-- celld-example: hello -->
13
14## API
15
16- `fetch(request, env, ctx)` is the default export. It receives the inbound
17 `Request` and returns a `Response`.
18- `scheduled` handles a Cron Trigger, and `queue` handles a Queues consumer.
19- `env` holds the bindings that the Wrangler configuration declares.
20- `ctx.waitUntil(promise)` keeps the isolate alive for the promise after the
21 response goes to the client.
22
23Read the
24[handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/)
25for the complete list.
26
27## The request path
28
29celld serves plain HTTP, so an ingress proxy must terminate TLS. A load
30balancer can send a request to any node, because every node holds the current
31deployment. That node runs the handler, and it forwards a binding call to the
32node that owns the cell. An R2 binding reads and writes the fleet bucket
33directly.
34
35![An ingress terminates TLS and sends a request to any celld node, which runs the Worker in a V8 isolate, while a binding call goes to the node that owns the cell or to the fleet bucket](workers-flow.svg)
36
37Each export of the main module must be a handler object or a class, as in
38workerd, so a main module that exports a string or a number fails to start.
39
40celld runs many requests in one isolate and retires isolates at any time, so
41a Worker must keep its state in a binding, not in module scope.
42
43## Differences from Cloudflare
44
45- celld does not manage a custom domain or terminate TLS.
46- `ctx.passThroughOnException()` has no effect, because celld has no CDN.
47- celld does not supply a Workers AI binding. The process rejects
48 `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a deployment rejects an `ai`
49 declaration.
50
51The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
52runtime APIs and the unsupported services.
53
54## Python Workers
55
56A Worker whose `main` is a `.py` file uses the
57[Cloudflare Python Workers](https://developers.cloudflare.com/workers/languages/python/)
58API. The same project runs on Cloudflare without a change. celld supports a
59part of that platform, which this section lists.
60
61The [Python example](../../examples/python) uses the SDK and a vendored
62package to handle a request.
63
64<!-- celld-example: python -->
65
66### Build a project
67
68Run `uv run pywrangler sync` to vendor the packages into `python_modules/`,
69then run `celld dev .`. The build rejects an incomplete Workers SDK
70(`workers-runtime-sdk`).
71
72`celld deploy` bundles the Pyodide runtime, `python_modules/`, and the files
73below the directory of `main` that match the default Wrangler module rules:
74`.py`, `.txt`, `.html`, `.sql`, `.bin`, and `.wasm`. The Worker can open each
75file under `/session/metadata`, as on Cloudflare. Nothing downloads at run
76time.
77
78The first build downloads the Pyodide runtime from the Pyodide CDN and checks
79the size and SHA-256 of each file. Later builds reuse
80`$XDG_CACHE_HOME/celld/pyodide-0.28.3`, or `~/.cache/celld/pyodide-0.28.3`.
81Set `CELLD_PYTHON_RUNTIME_DIR` to use another directory; a populated directory
82lets a build run offline. A download fails after 60 seconds without data. The
83build needs `esbuild` on `PATH`, or `CELLD_ESBUILD` set to its path.
84
85A Python deployment requires the `python-workers-v1` feature, and
86`celld deploy` does not check the nodes for it. A running node without the
87feature logs an error and keeps its current deployment, so the fleet serves
88two versions. A node without the feature that starts while a Python
89deployment is current exits with an error. Upgrade every node before the first
90Python deployment, and do not downgrade while one is current.
91
92### Runtime version
93
94celld runs Pyodide 0.28.3 with CPython 3.13.2 and the `pyodide_2025_0` wheel
95ABI. Cloudflare selects this line with `python_workers_20250116` (it runs
96Pyodide 0.28.2 with its own patches). The configuration must satisfy:
97
98- `compatibility_flags` includes `python_workers`.
99- `compatibility_date` has the form `YYYY-MM-DD`.
100- `compatibility_date` is 2026-04-21 or later. With an earlier date, a flag
101 must turn on each behavior that the date does not:
102 `python_workers_force_new_vendor_path` before 2025-08-11,
103 `python_no_global_handlers` before 2025-08-14, `python_workers_20250116`
104 before 2025-09-29, and `enable_python_external_sdk` before 2026-04-21.
105- `compatibility_date` is before 2026-09-08, or the flags include
106 `no_python_workers_314`. From 2026-09-08 Cloudflare runs Pyodide 314
107 (Python 3.14), which needs wheels for another ABI.
108
109celld refuses other settings at deploy time. `python_process_pth_files`
110follows its compatibility date (2026-05-26).
111
112### Supported
113
114| API | Notes |
115| --- | --- |
116| `Default(WorkerEntrypoint).fetch` | One instance for each request, as on Cloudflare |
117| `workers.Response`, `Response.json`, request body, text and headers | Compared with workerd in the tests |
118| `workers.fetch` | Outbound HTTP |
119| `self.env` bindings | Tested with KV. Other bindings are the objects a JavaScript Worker gets, untested from Python |
120| `self.ctx.waitUntil` | Python awaitables are kept alive until they settle |
121| Pure-Python packages in `python_modules/` | Tested with `beautifulsoup4` |
122| `from js import ...` | The JavaScript globals of the isolate |
123
124### Not supported
125
126- Durable Objects, Workflows, Cron Triggers and Queue consumers written
127 in Python. `celld deploy` refuses a Python project that declares them.
128- Named entrypoint classes and RPC to Python methods.
129- Packages with compiled extensions (`.so` files), and the standard
130 library modules that Pyodide ships as separate packages: `ssl`,
131 `sqlite3`, `lzma`, and the OpenSSL-backed `hashlib` algorithms.
132- `pyodide.ffi.run_sync`, and `workers.import_from_javascript` for
133 modules other than `cloudflare:workers` and `cloudflare:sockets`. celld
134 hides WebAssembly stack switching (JSPI), because under JSPI each async
135 entry leaks C stack until the interpreter stops.
136- `process.exit()` throws an error in a Python Worker.
137- Memory snapshots and the import patches of the `_cloudflare` package.
138 Package patches that need them, such as synchronous FastAPI handlers,
139 do not apply.
140
141### Failure behavior
142
143The interpreter starts on the first request to an isolate, and concurrent
144first requests share that start. If the main module raises an error, every
145request to that isolate fails with the same traceback.
146
147A fatal interpreter error, such as a C stack overflow, fails every request
148that the isolate is running. celld then replaces the isolate, as Cloudflare
149does, so the next request can wait for a new interpreter.