Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/content/docs/workers/configuration/cron-triggers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ export default {
</TabItem> <TabItem label="Python" icon="seti:python">

```python
from workers import WorkerEntrypoint, Response
from workers import WorkerEntrypoint

class Default(WorkerEntrypoint):
async def scheduled(self, controller, env, ctx):
print("cron processed")
# All four parameters (self, controller, env, ctx) are required
print("cron processed")
```

</TabItem></Tabs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export default {
} satisfies ExportedHandler<Env>;
```

</TabItem> <TabItem label="Python" icon="seti:python">

```python
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
# Environment variables are accessed via attribute access on self.env
return Response(f"API host: {self.env.API_HOST}")
```

</TabItem> </Tabs>

### Import `env` for global access
Expand Down
16 changes: 15 additions & 1 deletion src/content/docs/workers/languages/python/basics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ from hello import hello

class Default(WorkerEntrypoint):
async def fetch(self, request):
name = (await request.json()).name
body = await request.json()
name = body["name"]
return Response(hello(name))
```

Expand All @@ -71,6 +72,19 @@ curl --header "Content-Type: application/json" \
Hello, Python!
```

## Returning JSON Responses

To return JSON from a Python Worker, use `Response.json()`:

```python
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
data = {"message": "Hello", "status": "ok"}
return Response.json(data)
```

## The `env` Attribute

The `env` attribute on the `WorkerEntrypoint` can be used to access
Expand Down
21 changes: 18 additions & 3 deletions src/content/docs/workers/languages/python/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,23 @@ from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
name = (await request.json()).name
body = await request.json() # returns a native Python dict
name = body["name"]
return Response("Hello, {name}".format(name=name))
```

## Return a JSON response

```python
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
data = {"greeting": "Hello, World!", "status": "ok"}
# Use Response.json() to return JSON — do not pass a dict to Response() directly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Use Response.json() to return JSON — do not pass a dict to Response() directly

I guess this comment is unnecessary.

return Response.json(data)
```

## Read bundled asset files in your Worker

Let's say your Worker has the following structure:
Expand Down Expand Up @@ -200,11 +213,13 @@ Refer to [Durable Objects documentation](/durable-objects/get-started/) for more
## Cron Trigger

```python
from workers import WorkerEntrypoint, Response
from workers import WorkerEntrypoint

class Default(WorkerEntrypoint):
async def scheduled(self, controller, env, ctx):
print("cron processed")
# All four parameters (self, controller, env, ctx) are required —
# unlike fetch() which only takes (self, request).
Comment on lines +220 to +221

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# All four parameters (self, controller, env, ctx) are required —
# unlike fetch() which only takes (self, request).

This too?

print("cron processed")
```

Refer to [Cron Triggers documentation](/workers/configuration/cron-triggers/) for more information.
Expand Down
6 changes: 6 additions & 0 deletions src/content/docs/workers/languages/python/stdlib.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ The following are present but cannot be imported due to a dependency on the term

- pty
- tty

## In-memory filesystem

Python Workers have access to an ephemeral, in-memory filesystem. You can read and write files using standard Python file I/O (for example, `open()`, `pathlib.Path`), but all data is **lost when the Worker invocation ends**. The filesystem is not shared between invocations or instances.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not true. Files stored in the in-memory file system should be alive between invocations unless the isolate is aborted.


This can be useful for temporary file operations, but should not be relied upon for persistent storage. Use [KV](/kv/), [R2](/r2/), or [Durable Objects](/durable-objects/) for durable storage.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ export default {
</TabItem> <TabItem label="Python" icon="seti:python">

```python
from workers import WorkerEntrypoint, Response, fetch
from workers import WorkerEntrypoint

class Default(WorkerEntrypoint):
async def scheduled(self, controller, env, ctx):
await doSomeTaskOnASchedule()
# controller.cron contains the cron pattern that triggered this event
# controller.scheduledTime contains the scheduled time in ms since epoch
Comment on lines +61 to +62

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# controller.cron contains the cron pattern that triggered this event
# controller.scheduledTime contains the scheduled time in ms since epoch

This too? I guess AI agents are verbosely adding these kind of comment which I think is unnecessary to be included in the code example.

print(f"Cron triggered: {controller.cron}")
```

</TabItem></Tabs>
Expand Down