Live data from Hacker News

Python Cloudflare Workers

blog.cloudflare.com

21–30 of 101 posts

Re: Python Cloudflare Workers

#21

I'm curious to see how the limitation of using pyodide packages only will play out for non-trivial builds. Thinking of all the non-pure python code out there that need to be manually rebuilt to support a non-trivial production app. Maybe Cloudflare's adoption will help bring more packages into the fold, and if it's an 80/20 rule here, would be good enough.

I certainly think there's an 80/20 rule here. Most packages are not very hard to port, and generally the ones that are hard to build use features like threads and multiprocessing, graphics cards, raw sockets, green threads, or other capabilities that have no obvious analogue in a webassembly runtime.

As we mention in the blog post, the biggest issues are around supporting server and request packages since they are clearly useful in cloudflare workers but are difficult to port because they frequently use raw sockets and some form of concurrency.

Re: Python Cloudflare Workers

#22

This is kind of a game changer for running AI stuff on Cloudflare. Been hoping for this for a while now.

what is the maximum length a worker can run for? curious how this compares to AWS Lambda? or is it something completely different?

Re: Python Cloudflare Workers

#23

Earlier quoted context omitted.

>>Would love to port the whole Django app behind the site over, using their D1 database too. Is that wise? One DDOS attack could break your budget.

Only if the DDoS isn't blocked by the Cloudflare DDoS protection

what are the advantages of using Cloudflare db over supabase? So far im loving supabase but wasn't aware CF products have increased drastically

Rows read 5 million / day First 25 billion / month included + $0.001 / million rows

Rows written 100,000 / day First 50 million / month included + $1.00 / million rows

Storage (per GB stored) 5 GB (total) First 5 GB included + $0.75 / GB-mo

Re: Python Cloudflare Workers

#25

I with they added Azure Functions style workers using C# too, or AWS style lambdas using NativeAOT. Way lower runtime overhead and time to first response latency. But C# is an underdog language in those lands, so it's understandable.

At the moment, it seems like they're concentrating on using V8 isolates. In the article, there's a good diagram of why: an isolate is able to share so much between different applications. Even with NativeAOT, you're still launching an entire program that has to load everything into memory and execute.

In some ways, they're using V8 isolates the way that mod_php was used back in the day. One reason PHP became so dominant was because PHP was cheap and easy to deploy for small websites. Because the PHP runtime contained 90% of what a person wanted to do with PHP, your PHP code might be a small amount of code that mostly just called standard library functions like `mysql_query()`. If you were running a shared hosting service, you could have huge numbers of people running on the same box because every PHP script would be sharing a single instance of the PHP standard library - and that standard library was fast and written in C. If you wanted to offer Python hosting, each Python app would be duplicating the standard library they were using in memory and also needing lots of web packages that aren't part of the standard library (like a database package). So a minimal Python application was using tons more RAM because it wasn't sharing most of the code with everyone else on the box.

Even with NativeAOT, you're still duplicating a lot when running many different C# projects - as is the case with Go, Java, Ruby, etc. V8 isolates are this case where they tend to be lighter weight because so much can be shared between different users in the system.

In fact, the reason they're supporting Python is because Pyodide (Python interpreter in WASM) allows for dynamic linking. It means they can have a single Pyodide interpreter in memory that's shared by all the Python workers on the same box. Likewise, they can also share Python libraries that two different people on the same box might be using. They note that most languages that target WASM don't support dynamic linking and that the only way they can provide Cloudflare Workers at the price point they offer is because those Workers can share so much rather than duplicating and using more memory for each user.

If you really want C# on Workers, C# does support WASM.

Re: Python Cloudflare Workers

#26
post #23

Earlier quoted context omitted.

Only if the DDoS isn't blocked by the Cloudflare DDoS protection

what are the advantages of using Cloudflare db over supabase? So far im loving supabase but wasn't aware CF products have increased drastically Rows read 5 million / day First 25 billion / month included + $0.001 / million rows Rows written 100,000 / day First 50 million / month included + $1.00 / million rows Storage (per GB stored) 5 GB (total) First 5 GB included + $0.75 / GB-mo

Personally my favorite part of using D1 is that the database is managed the same way as everything else, and you just access it through a Workers binding rather than needing any authentication or connection strings or anything. I'm excited to see how the new session API and read replicas work too, since they might be able to reduce DB read latency to being within the same datacenter in many instances. But I only know about as much about the D1 session API and read replicas as anyone else that read the blog post about it.

Disclaimer: I work for Cloudflare, but not on Workers (my team just is a heavy user of Workers). I'm just speaking as a Workers user/enthusiast here.

Re: Python Cloudflare Workers

#27
post #2

A performance comparison to a JS worker would be helpful. It does sound interesting, but also sounds potentially slow, given all the layers involved. Not that I'm expecting parity, but knowing the rough tradeoff would be helpful.

Three aspects of performance:

1. Cold start perf 2. Post-cold start perf - The cost of bridging between JS and WebAssembly - The speed of the Python interpreter running in WebAssembly

Today, Python cold starts are slower than cold starts for a JavaScript Worker of equivalent size. A basic "Hello World" Worker written in JavaScript has a near zero cold start time, while a Python Worker has a cold start under 1 second.

That's because we still need to load Pyodide into your Worker on-demand when a request comes in. The blog post describes what we're working on to reduce this — making Pyodide already available upfront.

Once a Python Worker has gone through a cold start though, the differences are more on the margins — maybe a handful milliseconds, depending on what happens during the request.

- There is a slight cost (think — microseconds not milliseconds) to crossing the "bridge" between JavaScript and WebAssembly — for example, by performing I/O or async operations. This difference tends to be minimal — generally something measured in microseconds not milliseconds. People with performance sensitive Workers already write them in Rust https://github.com/cloudflare/workers-rs, which also relies on bridging between JavaScript and WebAssembly.

- The Python interpreter that Pyodide provides, that runs in WebAssembly, isn't as fast as the years and years of optimization that have gone into making JavaScript fast in V8. But it's still relatively early days for Pyodide, compared to the JS engine in V8 — there are parts of its code where we think there are big perf gains to be had. We're looking forward to upstreaming performance improvements, and there are WebAssembly proposals that help here too.

Re: Python Cloudflare Workers

#29
post #13

This is kind of a game changer for running AI stuff on Cloudflare. Been hoping for this for a while now.

> This is kind of a game changer for running AI stuff on Cloudflare. That certainly appears to be the intention. > Been hoping for this for a while now. You should check out the other two announcements from today as well if you haven't yet: "Leveling up Workers AI: General Availability and more new capabilities" https://blog.cloudflare.com/workers-ai-ga-huggingface-loras-... "Running fine-tuned models on Workers AI w…

Also, D1, Hyperdrive, and Workers Analytics Engine went GA.

https://blog.cloudflare.com/making-full-stack-easier-d1-ga-h...

Re: Python Cloudflare Workers

#30
post #11

I've played with JS workers on a Cloudflare-fronted site and found them to be easy to use and very quick. Would love to port the whole Django app behind the site over, using their D1 database too.

Agreed, this looks really cool. While there is no Django/DRF support at the moment, it does say that that they'll be increasing the number of packages in the future.

Would love feedback on which packages you'd like to see us support:

https://github.com/cloudflare/workerd/discussions/categories...

Post reply on HN