📱

Read on Your E-Reader

Thousands of readers get articles like this delivered straight to their e-reader. Works with Kindle, Boox, and any device that syncs with Google Drive or Dropbox.

Learn More

This is a preview. The full article is published at blog.cloudflare.com.

Python Workers redux: fast cold starts, packages, and a uv-first workflow

Python Workers redux: fast cold starts, packages, and a uv-first workflow

By Dominik PichetaThe Cloudflare Blog

Note: This post was updated with additional details regarding AWS Lambda. Last year we announced basic support for Python Workers , allowing Python developers to ship Python to region: Earth in a single command and take advantage of the Workers platform . Since then, we’ve been hard at work making the Python experience on Workers feel great. We’ve focused on bringing package support to the platform, a reality that’s now here - with exceptionally fast cold starts and a Python-native developer experience. This means a change in how packages are incorporated into a Python Worker. Instead of offering a limited set of built-in packages, we now support any package supported by Pyodide , the WebAssembly runtime powering Python Workers. This includes all pure Python packages, as well as many packages that rely on dynamic libraries. We also built tooling around uv to make package installation easy. We’ve also implemented dedicated memory snapshots to reduce cold start times. These snapshots result in serious speed improvements over other serverless Python vendors. In cold start tests using common packages, Cloudflare Workers start over 2.4x faster than AWS Lambda without SnapStart and 3x faster than Google Cloud Run . In this blog post, we’ll explain what makes Python Workers unique and share some of the technical details of how we’ve achieved the wins described above. But first, for those who may not be familiar with Workers or serverless platforms - and especially those coming from a Python background - let us share why you might want to use Workers at all. Deploying Python globally in 2 minutes Part of the magic of Workers is simple code and easy global deployments. Let's start by showing how you can deploy a FastAPI app across the world with fast cold starts in less than two minutes. A simple Worker using FastAPI can be implemented in a handful of lines: from fastapi import FastAPI from workers import WorkerEntrypoint import asgi app = FastAPI() @app.get("/") async def root(): return {"message": "This is FastAPI on Workers"} class Default(WorkerEntrypoint): async def fetch(self, request): return await asgi.fetch(app, request.js_object, self.env) To deploy something similar, just make sure you have uv and npm installed, then run the following: $ uv tool install workers-py $ pywrangler init --template \ https://github.com/cloudflare/python-workers-examples/03-fastapi $ pywrangler deploy With just a little code and a pywrangler deploy , you’ve now deployed your application across Cloudflare’s edge network that extends to 330 locations across 125 countries . No worrying about infrastructure or scaling. And for many use cases, Python Workers are completely free. Our free tier offers 100,000 requests per day and 10ms CPU time per invocation. For more information, check out the pricing page in our documentation . For more examples, check out the repo in GitHub . And read on to find out more about Python Workers. So what can you do with Python Workers? Now that you’ve got a Worker, just about anything is possible. You write the code, so you get to decide. Your Python Worker receives...

Preview: ~500 words

Continue reading at Cloudflare

Read Full Article

More from The Cloudflare Blog

Subscribe to get new articles from this feed on your e-reader.

View feed

This preview is provided for discovery purposes. Read the full article at blog.cloudflare.com. LibSpace is not affiliated with Cloudflare.

Python Workers redux: fast cold starts, packages, and a uv-first workflow | Read on Kindle | LibSpace