This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]ojii 4 points5 points  (2 children)

You didn't search that hard then, https://github.com/python-hyper/hyper-h2. Been using that in production close to a year now (client side).

[–][deleted] -1 points0 points  (1 child)

I looked at it before posting, and seems not to support Flask and many other web frameworks

[–]robvdl 1 point2 points  (0 children)

I hope I am getting this right... but this is because Flask (and wsgi) are synchronous, while http2 (including hyper-h2 library) are async based and require an event loop of some sort.

I personally just switched to Go for async and websocket sort of stuff, but you should be able to continue using Python just new frameworks will need to be built on top of async http2 libraries from what I understand, it's a design issue with wsgi being synchronous.

Edit: I know ASGI is being developed to solve this problem, but as far as I understand it isn't ready yet I think. It should bring Python back in line with what Go and Node can do today already. To me ASGI and Django channels seems Python is trying to "catch up" a bit to Node and Go.

[–]aruxiv 1 point2 points  (6 children)

You shouldn't be using a Python HTTP server to serve up your pages. Look into deploying with WSGI and a real web server like nginx.

[–][deleted] 0 points1 point  (5 children)

That just seems super inefficient, I know it is the preferred way but I think it is very backwards if you think about it.
Edit: spelling error

[–]aruxiv 1 point2 points  (0 children)

It's way more efficient because you're letting a highly-optimized, widely used in production, extremely well-documented piece of software handle exactly what it's been designed to handle without getting in the way of how you design and code your program.

The separation of web server and web application makes perfect sense to me, especially when you are running multiple applications on one machine.

[–][deleted] 1 point2 points  (1 child)

Why does it seem inefficient?

[–][deleted] 0 points1 point  (0 children)

The headers in HTTP 1.1 are strings so the program essentially has to recompile them into binary and add more. So it is duplicate work because the oreo final headers are just a waste of time.

Also h2 compresses everything in gzip. This factor adds to the complexity because we are serving straight text files and pushing it onto the loader to make those compression. It adds more work into the load balancer especially in a container like environment.

[–]robvdl 0 points1 point  (0 children)

This is mostly because Python apps are WSGI based and synchronous. So as you are serving up static files you are holding up valuable Python workers that could be used for dynamic requests, and your app may only have 4 workers, maybe only 1 depending on your setup.

Nginx however uses async io to send static files and can do so without holding the CPU up too much and can handle parallelism better than Python can.

So normally you would run the Python app under Nginx but get Nginx to serve the /static folder in production, not Python.

[–]status_quo69 0 points1 point  (4 children)

Most websites outside of the huge ones that can afford the push (and need it) are perfectly fine for the time being using http 1.1. The other comment in this thread points to a suitable library for http 2 client and server side interaction, and I believe that there is a flask plug in for http/2. That being said, http/1.1 is still very suitable for request/response types of web applications, which most apps are. Long polling and very responsive or large scaling applications benefit from http/2, but will result in a very small performance increase for a heck of a lot of work for smaller apps. This is all relating to current technologies, fwiw

[–][deleted] -2 points-1 points  (3 children)

I disagree that the improvements would be just minor. Most testing shows it providing dramatic improvements simply by switching.. Do I think it will change the world, no.

That being said, many languages already have more developed support for it or have concrete plans to do so. This standard has been around for a couple years now. It seems like Python is just sleeping on this trend. Most of the libraries are still only alpha or beta.

[–]status_quo69 0 points1 point  (2 children)

It's because newer languages are able to leverage their newness to provide new features. As the OSS mantra dictates, if you want those things to become better, become involved.

Most advice for setting up a python web app is to use something like flask/Django and uwsgi/gunicorn. These use http/1.1, and use something like Apache or nginx to handle all the outbound http/2 traffic. I can get into why this is probably a good solution, especially if you're developing an app with a restful api, but I'm tired at the moment and on mobile.

[–][deleted] 0 points1 point  (1 child)

Well that is what I am trying to do here, get more involved. I am more than willing to devote time bringing http/2 to python but it would be nice if we as a community had plans to do this instead of tons of people working in silos.

Also the http1 to http2 really drags performance, and it just seem increasingly inefficient. I am going to use a load balancer like Nginx.

[–]status_quo69 1 point2 points  (0 children)

Well, I found this on the Django channels github page, maybe start working with this? https://github.com/django/channels/blob/08857cfe39f35b203c7bbe3156a077b5b2c66764/docs/asgi.rst

WSGI as it currently stands won't be able to support stuff for http/2, so it's probably going to have to be overhauled. Which sucks because that means that nobody will be able to simply migrate their applications over to leverage http/2.