Skip to content
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

Running a Python Worker Silently "In The Background" While A Page Loads #14533

Open
erickbytes opened this issue May 12, 2024 · 1 comment · May be fixed by #14675
Open

Running a Python Worker Silently "In The Background" While A Page Loads #14533

erickbytes opened this issue May 12, 2024 · 1 comment · May be fixed by #14675
Assignees
Labels
content:edit Request for content edits documentation Documentation edits example product:workers Related to Workers product

Comments

@erickbytes
Copy link

erickbytes commented May 12, 2024

Existing documentation URL(s)

https://developers.cloudflare.com/workers/languages/python/examples/
https://developers.cloudflare.com/workers/examples/

What changes are you suggesting?

Explain how or demonstrate with an example how to trigger a Python Worker "in the background" with an AJAX script or concurrency so that it doesn't disrupt the normal page loading flow when attached to a route?

Additional information

I want to insert to the key value store via a worker simultaneously as the page loads. But when I attach the worker to a route with a routing rule, the response object is rendered to the browser. Working on a Python worker like this but running into too many redirect loops with fetch or redirects:

from js import Response, fetch
from datetime import datetime
import os

async def on_fetch(request, env):
    await env.KV_VALUE.put("stuff", str(datetime.now()))
    return await fetch(request.url)
@erickbytes erickbytes added content:edit Request for content edits documentation Documentation edits labels May 12, 2024
@github-actions github-actions bot added the product:workers Related to Workers product label May 12, 2024
@kodster28 kodster28 linked a pull request May 21, 2024 that will close this issue
@kodster28
Copy link
Contributor

Howdy @erickbytes, think the new example in #14675 about Modifying a Response might help you here (visible in the preview).

TL;DR, when you have an incoming request, you need to create a copy of the request and then modify whatever properties make sense to change.

@megaconfidence megaconfidence linked a pull request May 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content:edit Request for content edits documentation Documentation edits example product:workers Related to Workers product
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants