Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 11 additions & 12 deletions src/content/docs/workers/configuration/cron-triggers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ head: []
description: Enable your Worker to be executed on a schedule.
---

import { Render, WranglerConfig, TabItem, Tabs, DashButton } from "~/components";
import {
Render,
WranglerConfig,
TabItem,
Tabs,
DashButton,
} from "~/components";

## Background

Expand Down Expand Up @@ -55,11 +61,11 @@ export default {
</TabItem> <TabItem label="Python" icon="seti:python">

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

@handler
async def on_scheduled(controller, env, ctx):
print("cron processed")
class Default(WorkerEntrypoint):
async def scheduled(self, controller, env, ctx):
print("cron processed")
```

</TabItem></Tabs>
Expand Down Expand Up @@ -143,31 +149,24 @@ To avoid ambiguity you may prefer to use the three-letter abbreviations (e.g. `S
Some common time intervals that may be useful for setting up your Cron Trigger:

- `* * * * *`

- At every minute

- `*/30 * * * *`

- At every 30th minute

- `45 * * * *`

- On the 45th minute of every hour

- `0 17 * * sun` or `0 17 * * 1`

- 17:00 (UTC) on Sunday

- `10 7 * * mon-fri` or `10 7 * * 2-6`

- 07:10 (UTC) on weekdays

- `0 15 1 * *`

- 15:00 (UTC) on first day of the month

- `0 18 * * 6L` or `0 18 * * friL`

- 18:00 (UTC) on the last Friday of the month

- `59 23 LW * *`
Expand Down
61 changes: 30 additions & 31 deletions src/content/docs/workers/examples/cron-trigger.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

summary: Set a Cron Trigger for your Worker.
tags:
- Middleware
Expand Down Expand Up @@ -35,58 +34,58 @@ export default {
```ts
interface Env {}
export default {
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");
},
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");
},
};
```

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

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

@handler
async def on_scheduled(controller, env, ctx):
print("cron processed")
class Default(WorkerEntrypoint):
async def scheduled(self, controller, env, ctx):
print("cron processed")
```

</TabItem> <TabItem label="Hono" icon="seti:typescript">

```ts
import { Hono } from 'hono';
import { Hono } from "hono";

interface Env {}

// Create Hono app
const app = new Hono<{ Bindings: Env }>();

// Regular routes for normal HTTP requests
app.get('/', (c) => c.text('Hello World!'));
app.get("/", (c) => c.text("Hello World!"));

// Export both the app and a scheduled function
export default {
// The Hono app handles regular HTTP requests
fetch: app.fetch,

// The scheduled function handles Cron triggers
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");

// You could also perform actions like:
// - Fetching data from external APIs
// - Updating KV or Durable Object storage
// - Running maintenance tasks
// - Sending notifications
},
// The Hono app handles regular HTTP requests
fetch: app.fetch,

// The scheduled function handles Cron triggers
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");

// You could also perform actions like:
// - Fetching data from external APIs
// - Updating KV or Durable Object storage
// - Running maintenance tasks
// - Sending notifications
},
};
```

Expand Down
163 changes: 163 additions & 0 deletions src/content/docs/workers/languages/python/basics.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
pcx_content_type: concept
title: The Basics
sidebar:
order: 4
head:
- tag: title
content: Learn the basics of Python Workers
description: Learn the basics of Python Workers
---

import { WranglerConfig } from "~/components";

## Fetch Handler

As mentioned in the [introduction to Python Workers](/workers/languages/python/), a Python Worker can be as simple as four lines of code:

```python
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
return Response("Hello World!")
```

Similar to other Workers, the main entry point for a Python worker is the [`fetch` handler](/workers/runtime-apis/handlers/fetch) which handles incoming requests
sent to the Worker.

In a Python Worker, this handler is placed in a `Default` class that extends the `WorkerEntrypoint` class (which you can import from the `workers` SDK module).

## The `Request` Interface

The `request` parameter passed to your `fetch` handler is a JavaScript Request object, exposed via the [foreign function interface (FFI)](/workers/languages/python/ffi),
allowing you to access it directly from your Python code.

Let's try editing the worker to accept a POST request. We know from the
[documentation for `Request`](/workers/runtime-apis/request) that we can call
`await request.json()` within an `async` function to parse the request body as
JSON.

In a Python Worker, you would write:

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

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

Many other JavaScript APIs are available in Python Workers via the FFI, so you can
call other methods in a similar way.

Once you edit the `src/entry.py`, Wrangler will automatically restart the local
development server.

Now, if you send a POST request with the appropriate body,
your Worker will respond with a personalized message.

```bash
curl --header "Content-Type: application/json" \
--request POST \
--data '{"name": "Python"}' http://localhost:8787
```

```bash output
Hello, Python!
```

## The `env` Attribute

The `env` attribute on the `WorkerEntrypoint` can be used to access
[environment variables](/workers/configuration/environment-variables/),
[secrets](/workers/configuration/secrets/),and
[bindings](/workers/runtime-apis/bindings/).

For example, let us try setting and using an environment variable in a Python Worker. First, add the environment variable to your Worker's [Wrangler configuration file](/workers/wrangler/configuration/):

<WranglerConfig>

```toml
name = "hello-python-worker"
main = "src/entry.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2025-11-02"

[vars]
API_HOST = "example.com"
```

</WranglerConfig>

Then, you can access the `API_HOST` environment variable via the `env` parameter:

```python
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
async def fetch(self, request):
return Response(self.env.API_HOST)
```

## Modules

Python workers can be split across multiple files.

Let's create a new Python file, called `src/hello.py`:

```python
def hello(name):
return "Hello, " + name + "!"
```

Now, we can modify `src/entry.py` to make use of the new module.

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

class Default(WorkerEntrypoint):
async def fetch(self, request):
return Response(hello("World"))
```

Once you edit `src/entry.py`, Wrangler will automatically detect the change and
reload your Worker.

## Types and Autocompletion

When developing Python Workers, you can take advantage of type hints and autocompletion
in your IDE.

To enable them, install the `workers-runtime-sdk` package in your `pyproject.toml` file.

```toml
[dependency-groups]
dev = [
"workers-py",
"workers-runtime-sdk"
]
```

Additionally, you can generate types based on your Worker configuration using `uv run pywrangler types`

This includes Env types based on your bindings, module rules, and runtime types based on the compatibility_date
and compatibility_flags in your config file.

## Upgrading `pywrangler`

To upgrade to the latest version of `pywrangler`, run the following command:

```bash
uv tool upgrade workers-py
```

## Next Up

- Learn details about local development, deployment, and [how to Python Workers work](/workers/languages/python/how-python-workers-work).
- Explore the [package](/workers/languages/python/packages) docs for instructions on how to use packages with Python Workers.
- Understand which parts of the [Python Standard Library](/workers/languages/python/stdlib) are supported in Python Workers.
- Learn about Python Workers' [foreign function interface (FFI)](/workers/languages/python/ffi), and how to use it to work with [bindings](/workers/runtime-apis/bindings) and [Runtime APIs](/workers/runtime-apis/).
Loading
Loading