All Topics
All Topics
Technology
Technology
AI
AI
Business
Business
Entertainment
Entertainment
News
News
Programming
Programming
Security
Security
Science
Science
Design
Design
Environment
Environment
Finance
Finance
Crypto
Crypto
Politics
Politics
Sports
Sports
Education
Education
Gaming
Gaming
Art
Art
Music
Music
Health
Health
Books
Books
Food
Food
Travel
Travel
Personal
Personal
Bluesky
Twitter

Workers, Durable Objects - Durable Objects are now supported in Python Workers

1y ago

Source

CloudflareWorkers, Durable Objects - Durable Objects are now supported in Python Workerscloudflare.com
Snippet from the RSS feed
You can now create Durable Objects using Python Workers . A Durable Object is a special kind of Cloudflare Worker which uniquely combines compute with storage, enabling stateful long-running applications which run close to your users. For more info see here . You can define a Durable Object in Python in a similar way to JavaScript: from workers import DurableObject , Response , WorkerEntrypoint from urllib . parse import urlparse class MyDurableObject ( DurableObject ): def __init__ ( self , ctx , env ): self . ctx = ctx self . env = env def fetch ( self , request ): result = self . ctx . storage . sql . exec ( "SELECT 'Hello, World!' as greeting" ). one () return Response ( result . greeting ) class Default ( WorkerEntrypoint ): async def fetch ( self , request ): url = urlparse ( request . url ) id = env . MY_DURABLE_OBJECT . idFromName ( url . path ) stub = env . MY_DURABLE_OBJECT . get ( id ) greeting = await stub . fetch ( request . url ) return greeting Define the Durable Object in your Wrangler configuration file: wrangler.jsonc { " durable_objects " : { " bindings " : [ { " name " : "MY_DURABLE_OBJECT" , " class_name " : "MyDurableObject" } ] } } wrangler.toml [[ durable_objects . bindings ]] name = "MY_DURABLE_OBJECT" class_name = "MyDurableObject" Then define the storage backend for your Durable Object: wrangler.jsonc { " migrations " : [ { " tag " : "v1" , // Should be unique for each entry " new_sqlite_classes " : [ // Array of new classes "MyDurableObject" ] } ] } wrangler.toml [[ migrations ]] tag = "v1" new_sqlite_classes = [ "MyDurableObject" ] Then test your new Durable Object locally by running wrangler dev : npx wrangler dev Consult the Durable Objects documentation for more details.

You might also wanna read

Comments

Sign in to join the conversation.

No comments yet. Be the first.