News for hackers. Stuff that matters.
Re:Python 3.13 Gets a JIT by mg on Tuesday January 09, 2024 @06:31AM (#38924896)
I love Python and use it for everything other than web development.

One reason is performance. So if Python has a faster future ahead of it: Hurray!

The other reason is that the Python ecosystem moved away from stateless requests like CGI or mod_php use and now is completely set on long running processes.

Does this still mean you have to restart your local web application after any change you made to it? I heard that some developers automate that, so that everytime they save a file, the web application is restarted. That seems pretty expensive in terms of resource consumption. And complex as you would have to run some kind of watcher process which handles watching your files and restarting the application?

Re:Python 3.13 Gets a JIT by onetoo on Tuesday January 09, 2024 @06:36AM (#38924930)
The restart isn't expensive in absolute terms, on a human level it's practically instant. You would only do this during development, hopefully your local machine isn't the production environment.

It's also very easy, often just adding a CLI flag to your local run command.

edit: Regarding performance, Python today can easily handle at least 1k requests per second. The vast vast vast majority of web applications today don't need anywhere near that kind of performance.

Re:Python 3.13 Gets a JIT by ubercore on Tuesday January 09, 2024 @06:54AM (#38925066)
Been working with python for the web for over a decade. This is basically a solved issue, and the performance is a non-issue day to day.
Re:Python 3.13 Gets a JIT by mg on Tuesday January 09, 2024 @09:18AM (#38926435)
The thing is, I don't run my applications locally with a "local run command".

I prefer to have a local system set up just like the production server, but in a container.

Maybe using WSGI with MaxConnectionsPerChild=1 could be a solution? But that would start a new (for example) Django instance for every request. Not sure how fast Django starts.

Another option might be to send a HUP signal to Apache:

    apachectl -k restart
That will only kill the worker threats. And when there are none (because another file save triggered it already), this operation might be almost free in terms of resource usage. This also would require WSGI or similar. Not sure if that is the standard approach for Django+Apache.
Re:Python 3.13 Gets a JIT by onetoo on Tuesday January 09, 2024 @10:26AM (#38927195)
I would still recommend running it properly locally, but whatever. Pseudo-devcontainer it is. I assume the code is properly volume mounted.

In production, you would want to run your app through gunicorn/uvicorn/whatever on an internal-only port, and reverse-proxy to it with a public-facing apache or similar.

Set up apache to reverse proxy like you would on prod, and run gunicorn/uvicorn l/whatever like you would on prod, except you also add the autoreload flag. E.g.

    uvicorn main:app --host 0.0.0.0 --port 12345 --reload
If production uses containers, you should keep the python image slim and simple, including only gunicorn/uvicorn and have the reverse proxy in another container. Etc.
Re:Python 3.13 Gets a JIT by traverseda on Tuesday January 09, 2024 @01:41PM (#38930098)
Is the problem you're having that you feel the need to expose a WSGI/ASGI interface instead of just a reverse proxy? Take a look at gunicorn, and for static files server you can use whitenoise.

With those two you can just stand up an python program in a container that serves html, and put it behind whatever reverse proxy you want.

Re:Python 3.13 Gets a JIT by edgyquant on Tuesday January 09, 2024 @03:42PM (#38931813)
I hate this argument that “most web apps don’t need that kind of performance.” For one thing, with responsive apps that are the norm it wouldn’t be surprising for a session to begin with multiple requests or to even have multiple requests per second. At that point all it takes is a few hundred active users to hit that 1k limit.

But even leaving that aside, you never know when your application will be linked somewhere or go semi-viral and not being able to serve 1000 users is all it takes for your app to go down and your one shot at a successful company to die a sad death.

Re:Python 3.13 Gets a JIT by onetoo on Tuesday January 09, 2024 @03:50PM (#38931912)
I didn't say python can handle <=1K, I was saying >=1K. I feel confident that I am orders of magnitude off the real limit you'd meet.

The specifics of that aside, any unprepared application is going to buckle at a sudden mega-surge of users. The solution remains largely the same, regardless of technology: Make sure everything that can be cached is cached, scale the hardware vertically until it stops helping, optimize your code, scale horizontally until you run out of money. I imagine the DB will be the actual bottleneck, most of the time.

There are other reasons to not choose python for greenfield application, but performance should rarely be one IMO.

Re:Python 3.13 Gets a JIT by kstrauser on Wednesday January 10, 2024 @01:25PM (#38941539)
At one shop, our load tests targeted 300,000 POST requests per second with a relatively small number of load balanced servers.

You wouldn’t want to have a long, complex call path through that code, but just parsing the body and adding it to a Celery queue before returning a 201 Created was perfectly manageable.

Re:Python 3.13 Gets a JIT by BiteCode_dev on Tuesday January 09, 2024 @07:48AM (#38925543)
In dev, this is handled mostly by the OS with things like inotify, so it has little perf impact.

In prod, you don't do it. Deployment implies sending a signal like HUP to your app, so that it reloads the code gracefully.

All in all, everybody is moving to thid, even php. This allows for persitent connexion, function memoization, delegation to threadpools, etc

Re:Python 3.13 Gets a JIT by neurostimulant on Tuesday January 09, 2024 @06:40AM (#38924964)
If you run the debug web server, (e.g. Django's `manage.py runserver`) command, yes it has watcher that will automatically restart the web server process if there is a code changes.

Once you deploy it to production, you usually run it using a WSGI/ASGI server such as Gunicorn or Uvicorn and let whatever deployment process you use handles the lifecycle. You usually don't use watcher in production.

Basically similar stuff with nodejs, rails, etc.

Re:Python 3.13 Gets a JIT by gklitz on Tuesday January 09, 2024 @10:06AM (#38926992)
> That seems pretty expensive in terms of resource consumption. And complex as you would have to run some kind of watcher process which handles watching your files and restarting the application?

What? No, in reality it’s just running your app in debug mode (just a cli flag), and when you save the files the next refresh of the browser has the live version of the app. It’s neither expensive nor complex.

Re:Python 3.13 Gets a JIT by swingingFlyFish on Tuesday January 09, 2024 @12:01PM (#38928518)
Python is amazing and shines for Web development. I'd recommend taking a look at https://www.tornadoweb.org/en/stable/index.html. I use this in production on my pet project at https://www.meecal.co/. Put Nginx in front and you're golden.

Definitely take a look, it's come a long way from ten years ago.

Re:Python 3.13 Gets a JIT by aftbit on Tuesday January 09, 2024 @02:12PM (#38930531)
I personally like Quart, which is like Flask, but with asyncio. Django is also incredibly popular and has been around forever, so it is very battle-tested.
Re:Python 3.13 Gets a JIT by qwertox on Tuesday January 09, 2024 @06:18PM (#38933747)
aiohttp for the win.

I started with cherrypy, then used twisted for years and finally aiohttp made me never look back or search for anything else.

Re:Python 3.13 Gets a JIT by treyd on Tuesday January 09, 2024 @06:33PM (#38933912)
I've been using FastAPI which is made by the same guy as Flask but taking it seriously this time and using asyncio and making space for multithreaded. It's almost a drop-in replacement for Flask.
Re:Python 3.13 Gets a JIT by herman_toothrot on Tuesday January 09, 2024 @07:30PM (#38934472)
FastAPI was created by Sebastián Ramírez (tiangolo) and flask was created by Armin Ronacher.
Re:Python 3.13 Gets a JIT by yla92 on Tuesday January 09, 2024 @09:16PM (#38935320)
Love Tornado and use them for everything backend at 99.co
Re:Python 3.13 Gets a JIT by acdha on Tuesday January 09, 2024 @04:01PM (#38932054)
> Does this still mean you have to restart your local web application after any change you made to it? I heard that some developers automate that, so that everytime they save a file, the web application is restarted. That seems pretty expensive in terms of resource consumption.

All of the popular frameworks automatically reload. It’s not instantaneous but with e.g. Django it was less than the time I needed to switch windows a decade ago and it hadn’t gotten worse. If you’re used to things like NextJS it will likely be noticeably faster.

Re:Python 3.13 Gets a JIT by qwertox on Tuesday January 09, 2024 @05:32PM (#38933222)
With `reload(module)` you don't even have to restart the server if you structure it properly.

Think server.py and server_handlers.py, where server.py contains logic to detect a modification of server_handlers.py (like via inotify) and the base handlers which then call the "modifiable" handlers in server_handlers.py.

This is not limited to servers (anything that loops or reacts to events) and can be nested multiple levels deep and is among the top 3 reasons of why i use Python.

Reloading is instantaneous and can gracefully handle errors in the file (just print an err messge or stack trace and keep running the old code)

Re:Python 3.13 Gets a JIT by declaredapple on Tuesday January 09, 2024 @04:05PM (#38932083)
> The other reason is that the Python ecosystem moved away from stateless requests like CGI or mod_php use and now is completely set on long running processes.

The long-running process is a WSGI/ASGI process that handles spawning the actual code, similar to CGI. The benefit is that it can handle how it spawns the request workers via multiple runtimes, process/threads, etc. It's similar to CGI but instead of nginx handling it, it's a special program that specializes in the different options for python specifically.

> Does this still mean you have to restart your local web application after any change you made to it? I heard that some developers automate that, so that everytime they save a file, the web application is restarted. That seems pretty expensive in terms of resource consumption. And complex as you would have to run some kind of watcher process which handles watching your files and restarting the application?

Only for development!

To update your code in production you first deploy the new code onto the machine, and then you tell the WSGI/ASGI such as Gunicorn to reload. This will cause it to use the new code for new request, without killing current requests.

It's a graceful reload, with no file watching needed. Just a "systemctl reload gunicorn"

Re:Python 3.13 Gets a JIT by paulddraper on Tuesday January 09, 2024 @08:33PM (#38934998)
> That seems pretty expensive in terms of resource consumption.

As opposed to CGI?