-
Notifications
You must be signed in to change notification settings - Fork 15k
Python Workers docs/examples fixes. #31399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: production
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
| return Response.json(data) | ||||||
| ``` | ||||||
|
|
||||||
| ## Read bundled asset files in your Worker | ||||||
|
|
||||||
| Let's say your Worker has the following structure: | ||||||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This too? |
||||||
| print("cron processed") | ||||||
| ``` | ||||||
|
|
||||||
| Refer to [Cron Triggers documentation](/workers/configuration/cron-triggers/) for more information. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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> | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this comment is unnecessary.