| 1 | # Workers |
| 2 | |
| 3 | A Worker is a stateless request handler that uses the Cloudflare Workers API. |
| 4 | An application uses a Worker for the code that answers an HTTP request, such as |
| 5 | an API endpoint, a router, a webhook receiver, or a front end for a binding. |
| 6 | celld runs the deployment in a V8 isolate on a node of the fleet, so a Worker |
| 7 | keeps the persistent state of an application in a binding instead. Read the |
| 8 | [Cloudflare Workers documentation](https://developers.cloudflare.com/workers/runtime-apis/) |
| 9 | for the standard API behavior. |
| 10 | |
| 11 | ## Example |
| 12 | |
| 13 | The [hello example](../../examples/hello) returns a text response from a |
| 14 | `fetch` handler. |
| 15 | |
| 16 | <!-- celld-example: hello --> |
| 17 | |
| 18 | ## The request path |
| 19 | |
| 20 | An operator puts an ingress proxy or a load balancer in front of the fleet. |
| 21 | The ingress terminates TLS, because celld serves plain HTTP on the public |
| 22 | listener of a node. The load balancer can send a request to any node in its |
| 23 | rotation, and every node embeds V8 and holds the current deployment. The node |
| 24 | that accepts the request therefore runs the handler itself, and it forwards |
| 25 | only a binding call that a different node owns. |
| 26 | |
| 27 | Cloudflare instead routes a request through its own global network and starts |
| 28 | the Worker in a nearby data center. A celld fleet has only the machines that |
| 29 | you run, so you control the placement and the routing. Read |
| 30 | [How Workers works](https://developers.cloudflare.com/workers/reference/how-workers-works/) |
| 31 | for the Cloudflare model. |
| 32 | |
| 33 |  |
| 34 | |
| 35 | ## The handler |
| 36 | |
| 37 | An HTTP request invokes the `fetch(request, env, ctx)` handler that the module |
| 38 | exports as its default export. The handler receives the inbound `Request`, and |
| 39 | it must return a `Response`. A module can export more handlers, such as |
| 40 | `scheduled` for a Cron Trigger or `queue` for a Queues consumer. Each export of |
| 41 | the main module must be a handler object or a class, as in workerd. A Worker |
| 42 | whose main module exports a string or a number therefore fails to start. Read |
| 43 | the |
| 44 | [handlers documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/) |
| 45 | for the complete list, and the |
| 46 | [fetch handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/) |
| 47 | for the parameters. |
| 48 | |
| 49 | The `ctx` argument controls the lifetime of the invocation. `ctx.waitUntil()` |
| 50 | keeps the isolate alive for a promise after the response goes to the client. |
| 51 | `ctx.passThroughOnException()` has no effect on celld, because celld has no CDN |
| 52 | that can answer in place of a failed Worker. |
| 53 | |
| 54 | ## State and bindings |
| 55 | |
| 56 | The `env` argument contains the bindings that the Wrangler configuration |
| 57 | declares. A binding gives the Worker a permission and an API together, so the |
| 58 | code needs no credential for the target service. Read the |
| 59 | [bindings documentation](https://developers.cloudflare.com/workers/runtime-apis/bindings/) |
| 60 | for the configuration syntax. |
| 61 | |
| 62 | A binding call leaves the isolate, and the target decides where the call goes. |
| 63 | A KV namespace, a D1 database, a queue, and a Durable Object are each a cell, |
| 64 | and one node owns a cell at a time, so celld routes the call to that owner. An |
| 65 | R2 binding has no cell, and it reads and writes the fleet bucket directly. |
| 66 | |
| 67 | celld runs many requests in one isolate, so a value in the module scope can |
| 68 | still be present for a later request. That value is not a safe place for |
| 69 | application state. The load balancer can send the next request to a different |
| 70 | node, and celld retires an isolate when the request load falls or when a new |
| 71 | deployment supersedes it, so the value can also disappear. A Worker must |
| 72 | therefore not depend on mutable in-memory state, and it must write the state |
| 73 | that it must keep into a binding. The Cloudflare |
| 74 | [best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/) |
| 75 | give the same rule. |
| 76 | |
| 77 | ## Differences from Cloudflare |
| 78 | |
| 79 | - celld does not manage a custom domain or terminate TLS. Terminate TLS in the |
| 80 | ingress proxy. |
| 81 | - celld does not supply a Workers AI binding. Call an AI provider from the |
| 82 | application. The process rejects `CELLD_AI_BINDING` and `CELLD_AI_URL`, and a |
| 83 | deployment rejects an `ai` declaration. |
| 84 | |
| 85 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 86 | runtime APIs and the unsupported services. |
| 87 | |
| 88 | ## Python Workers |
| 89 | |
| 90 | A Worker whose `main` is a `.py` file runs on celld with the |
| 91 | [Cloudflare Python Workers](https://developers.cloudflare.com/workers/languages/python/) |
| 92 | API: a `Default` class that extends `WorkerEntrypoint`, the `Response` |
| 93 | and `fetch` of the `workers` package, and bindings on `self.env`. The same |
| 94 | project runs on Cloudflare without a change. |
| 95 | |
| 96 | The [Python example](../../examples/python) uses the SDK and a vendored |
| 97 | package to handle a request. |
| 98 | |
| 99 | <!-- celld-example: python --> |
| 100 | |
| 101 | celld supports a bounded part of the Python Workers platform. This page |
| 102 | lists what that part is. |
| 103 | |
| 104 | ### Build a project |
| 105 | |
| 106 | Run `uv run pywrangler sync` to vendor the project's packages with |
| 107 | Cloudflare's `pywrangler`. Then run `celld dev .` to start the project. |
| 108 | |
| 109 | `pywrangler sync` writes the packages to `python_modules/`, including the |
| 110 | Workers SDK (`workers-runtime-sdk`). The build rejects an incomplete Workers |
| 111 | SDK. `celld deploy` embeds the files below the directory of `main` that |
| 112 | Wrangler uploads with its default module rules: `.py`, `.txt`, `.html`, |
| 113 | `.sql`, `.bin`, and `.wasm` files. It also embeds the files of |
| 114 | `python_modules/`, and puts all of them in one bundle with the Pyodide |
| 115 | runtime. The Worker can open each embedded file under `/session/metadata`, |
| 116 | as on Cloudflare. Nothing is downloaded when the Worker runs. |
| 117 | |
| 118 | The first build downloads the Pyodide runtime files from the Pyodide CDN |
| 119 | and checks the size and the SHA-256 of each one. Later builds reuse them |
| 120 | from `$XDG_CACHE_HOME/celld/pyodide-0.28.3`, or from |
| 121 | `~/.cache/celld/pyodide-0.28.3` when `XDG_CACHE_HOME` is not set. Set |
| 122 | `CELLD_PYTHON_RUNTIME_DIR` to use another directory; a directory that |
| 123 | already holds the files lets a build run offline. A download fails when the |
| 124 | CDN sends no data for 60 seconds, so a stalled network does not stop the |
| 125 | build without an error. The build needs `esbuild` on `PATH`, as a JavaScript build |
| 126 | does, or `CELLD_ESBUILD` set to its path. |
| 127 | |
| 128 | A Python deployment requires the `python-workers-v1` feature. |
| 129 | `celld deploy` does not examine the nodes of the fleet, so the |
| 130 | deployment succeeds even when a node does not have the feature. A running node that |
| 131 | predates the feature logs an error and continues to serve its current |
| 132 | deployment, so the fleet then serves two versions. A node that predates |
| 133 | the feature and starts while a Python deployment is current exits with |
| 134 | an error. Therefore, upgrade every node before the first Python |
| 135 | deployment, and do not replace this celld version with an earlier one |
| 136 | while a Python deployment is current. |
| 137 | |
| 138 | ### Runtime version |
| 139 | |
| 140 | celld runs Pyodide 0.28.3 with CPython 3.13.2 and the `pyodide_2025_0` |
| 141 | wheel ABI. This is the runtime line that Cloudflare selects with |
| 142 | `python_workers_20250116` (Cloudflare runs Pyodide 0.28.2 with its own |
| 143 | patches). The configuration must select that line and the vendored SDK: |
| 144 | |
| 145 | - `compatibility_flags` includes `python_workers`. |
| 146 | - `compatibility_date` has the form `YYYY-MM-DD`. |
| 147 | - `compatibility_date` is 2026-04-21 or later, which turns on |
| 148 | `enable_python_external_sdk`, `python_no_global_handlers`, and the |
| 149 | `python_modules` search path. With an earlier date, a flag must turn on |
| 150 | each behavior that the date does not turn on: |
| 151 | `python_workers_force_new_vendor_path` before 2025-08-11, |
| 152 | `python_no_global_handlers` before 2025-08-14, `python_workers_20250116` |
| 153 | before 2025-09-29, and `enable_python_external_sdk` before 2026-04-21. |
| 154 | - `compatibility_date` is before 2026-09-08, or the flags include |
| 155 | `no_python_workers_314`. From 2026-09-08 Cloudflare runs Pyodide 314 |
| 156 | (Python 3.14), which needs wheels for another ABI. |
| 157 | |
| 158 | celld refuses other settings at deploy time. `python_process_pth_files` |
| 159 | follows its compatibility date (2026-05-26). |
| 160 | |
| 161 | ### Supported |
| 162 | |
| 163 | | API | Notes | |
| 164 | | --- | --- | |
| 165 | | `Default(WorkerEntrypoint).fetch` | One instance for each request, as on Cloudflare | |
| 166 | | `workers.Response`, `Response.json`, request body, text and headers | Compared with workerd in the tests | |
| 167 | | `workers.fetch` | Outbound HTTP | |
| 168 | | `self.env` bindings | Tested with KV. Other bindings are the objects a JavaScript Worker gets, untested from Python | |
| 169 | | `self.ctx.waitUntil` | Python awaitables are kept alive until they settle | |
| 170 | | Pure-Python packages in `python_modules/` | Tested with `beautifulsoup4` | |
| 171 | | `from js import ...` | The JavaScript globals of the isolate | |
| 172 | |
| 173 | ### Not supported |
| 174 | |
| 175 | - Durable Objects, Workflows, Cron Triggers and Queue consumers written |
| 176 | in Python. `celld deploy` refuses a Python project that declares them. |
| 177 | - Named entrypoint classes and RPC to Python methods. |
| 178 | - Packages with compiled extensions (`.so` files), and the standard |
| 179 | library modules that Pyodide ships as separate packages: `ssl`, |
| 180 | `sqlite3`, `lzma`, and the OpenSSL-backed `hashlib` algorithms. |
| 181 | - `pyodide.ffi.run_sync`, and `workers.import_from_javascript` for |
| 182 | modules other than `cloudflare:workers` and `cloudflare:sockets`. celld |
| 183 | hides WebAssembly stack switching (JSPI) from the runtime, because under |
| 184 | JSPI each async entry leaks C stack until the interpreter stops. |
| 185 | - `process.exit()` throws an error in a Python Worker. |
| 186 | - Memory snapshots and the import patches of the `_cloudflare` package. |
| 187 | Package patches that need them, such as synchronous FastAPI handlers, |
| 188 | do not apply. |
| 189 | |
| 190 | ### Failure behavior |
| 191 | |
| 192 | The interpreter starts on the first request that an isolate handles, and |
| 193 | concurrent first requests share that start. If the main module raises an |
| 194 | error, every request to that isolate fails with the same traceback. |
| 195 | |
| 196 | A fatal interpreter error, such as a C stack overflow from deep |
| 197 | recursion, fails the request in flight and the other requests that the |
| 198 | isolate is running. celld then replaces the isolate, as Cloudflare does. |
| 199 | The next request goes to another isolate, so it can wait for a new |
| 200 | interpreter to start. |