Skip to content

Commit d0920ce

Browse files
authored
Workers: add Python Durable Object changelog. (#22411)
1 parent f001400 commit d0920ce

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Durable Objects are now supported in Python Workers
3+
description: You can now create Durable Objects using Python
4+
products:
5+
- workers
6+
- durable-objects
7+
date: 2025-05-16T12:00:00Z
8+
---
9+
10+
import { WranglerConfig } from "~/components";
11+
12+
You can now create [Durable Objects](/durable-objects/) using
13+
[Python Workers](/workers/languages/python/). A Durable Object is a special kind of
14+
Cloudflare Worker which uniquely combines compute with storage, enabling stateful
15+
long-running applications which run close to your users. For more info see
16+
[here](https://developers.cloudflare.com/durable-objects/what-are-durable-objects/).
17+
18+
You can define a Durable Object in Python in a similar way to JavaScript:
19+
20+
```python
21+
from workers import DurableObject, Response, handler
22+
23+
from urllib.parse import urlparse
24+
25+
class MyDurableObject(DurableObject):
26+
def __init__(self, ctx, env):
27+
self.ctx = ctx
28+
self.env = env
29+
30+
def on_fetch(self, request):
31+
result = self.ctx.storage.sql.exec("SELECT 'Hello, World!' as greeting").one()
32+
return Response(result.greeting)
33+
34+
@handler
35+
async def on_fetch(request, env, ctx):
36+
url = urlparse(request.url)
37+
id = env.MY_DURABLE_OBJECT.idFromName(url.path)
38+
stub = env.MY_DURABLE_OBJECT.get(id)
39+
greeting = await stub.fetch(request.url)
40+
return greeting
41+
```
42+
43+
Define the Durable Object in your Wrangler configuration file:
44+
45+
<WranglerConfig>
46+
47+
```toml
48+
[[durable_objects.bindings]]
49+
name = "MY_DURABLE_OBJECT"
50+
class_name = "MyDurableObject"
51+
```
52+
53+
</WranglerConfig>
54+
55+
Then define the storage backend for your Durable Object:
56+
57+
<WranglerConfig>
58+
59+
```toml
60+
[[migrations]]
61+
tag = "v1" # Should be unique for each entry
62+
new_sqlite_classes = ["MyDurableObject"] # Array of new classes
63+
```
64+
65+
</WranglerConfig>
66+
67+
Then test your new Durable Object locally by running `wrangler dev`:
68+
69+
```
70+
npx wrangler dev
71+
```
72+
73+
Consult the [Durable Objects documentation](/durable-objects/) for more details.

0 commit comments

Comments
 (0)