Live data from Hacker News

Types for Python HTTP APIs

instagram-engineering.com

31–40 of 55 posts

Re: Types for Python HTTP APIs

#31
post #2

Looks like FastAPI framework with handler type anotations

Exactly what I was thinking.

Plus the fact it is built on top of Starlette and Uvicorn and is frequently among the top of benchmarks of python based framework performance.

Fairly complete starter projects.

https://github.com/tiangolo/full-stack-fastapi-postgresql/bl...

Re: Types for Python HTTP APIs

#32

I find it really impressive that such a large organization runs anything on Python. Isn't the speed optimization potential immense? Does the amount of Python involved just not matter compared to image data? Secondly I find it really impressive that such a large company with so many smart people can produce an application so mediocre and make the experience extra terrible and me wonder what the absolute fuck is up wit…

Who cares if it's 10% slower running on Python if the whole thing is going to be distributed across 1000s of containers that are behind layers of caching. At that point developer productivity is more important than runtime performance.

What exactly are those developers doing all day then? Because every time I open an Instagram.com link I'm struck by how bad it is. The images are small, the comment list is the same height as the image, so wide but short images have short comment list, the comments are scrolled to the bottom so the poster's description isn't visible without scrolling up, videos doesn't have any kind of playback controls so you can't even see how long they are, and so on. It's a terrible web site and I don't understand where all the developer time and productivity is going.

And wouldn't a 10% hardware cost decrease be worth a lot at a company with such a high load, or is it all rendered so seldom because of the caching?

Re: Types for Python HTTP APIs

#33
This is a bit lower level as it's only concerned with serializing, but a package I wrote called json_syntax[1] will take @dataclass or @attr.s classes decorated with type annotations and build encoders and decoders for them.

It also handles Union types reasonably well and lets you put in hooks to handle ugly cases. It's used in production on a system with a big complicated payload, and I designed it to be easily extensible if the standard rules don't work for you.

[1]: https://pypi.org/project/json-syntax/

Re: Types for Python HTTP APIs

#34

I find it really impressive that such a large organization runs anything on Python. Isn't the speed optimization potential immense? Does the amount of Python involved just not matter compared to image data? Secondly I find it really impressive that such a large company with so many smart people can produce an application so mediocre and make the experience extra terrible and me wonder what the absolute fuck is up wit…

> I find it really impressive that such a large organization runs anything on Python. Isn't the speed optimization potential immense? Does the amount of Python involved just not matter compared to image data?

Usually, that's exactly the story: there are certain hot spots that account for the vast majority of your processing time.

I work on an optimization engine that evaluates financial plans.

We have a ton of business logic that is not performance sensitive, and we decompose those objects into flat, regular primitives so they can run in a tight loop that does the actual simulation.

We optimized that using numba and get quite acceptable performance.

But let me revisit what you asked:

> I find it really impressive that such a large organization

In a large organization, you need to find people with the right skillset, and if someone is a subject matter expert (mathematician, statistician, etc.) they often know Python. If they can read and understand your code, they can directly check it for correctness.

Or they can load modules into Jupyter and work with them. Have a lingua franca is itself very powerful.

Re: Types for Python HTTP APIs

#36

In our startup we use marshmallow [1] to validate and make REST API type aware. So marshmallow models validates the data types based on validation rules for request/response. In place where we need client to be able to define the validation the request/response will consist of data, validation json schema. In this case use jsonschema validator [2] [3]. So for every json request/request data, it's passed through a mar…

Marshmallow is good but it can be slow. We also use it over a Flask API for input/output serialization and in the worse cases it can take a significant amount of time if the objects are large enough (maybe a hundred milliseconds). We also use the Marshmallow models in conjunction with a project called `flask-apispec` by the same authors to generate Swagger docs. I've wanted to explore using the Typing module to repla…

We are exploring using Python data classes and typehints directly. This way can remove all dependencies and rely on standard library.

As first step we are exploring marshmallow with data classes like the way we are dealing with jsonschema using pydantic.

In our product we prefer to use as little 3rd party packages as possible and rely on standard library. When we want to use 3rd party package we look at the code and if my team can support and enhance it then only we use, except in some case where package is better than standard library like requests.

Re: Types for Python HTTP APIs

#37

Earlier quoted context omitted.

Who cares if it's 10% slower running on Python if the whole thing is going to be distributed across 1000s of containers that are behind layers of caching. At that point developer productivity is more important than runtime performance.

What exactly are those developers doing all day then? Because every time I open an Instagram.com link I'm struck by how bad it is. The images are small, the comment list is the same height as the image, so wide but short images have short comment list, the comments are scrolled to the bottom so the poster's description isn't visible without scrolling up, videos doesn't have any kind of playback controls so you can't…

I'm not sure I can say this with authority, but no one cares about the instagram website. It's primarily a mobile app, so they've been ignoring development on the website's bugs for as long as I can remember using it.

Besides most of those problems are UI-related, this article is about the backend and really python is perfectly fine for a backend that has a huge focus on data (for obvious reasons)

Re: Types for Python HTTP APIs

#39

Earlier quoted context omitted.

Who cares if it's 10% slower running on Python if the whole thing is going to be distributed across 1000s of containers that are behind layers of caching. At that point developer productivity is more important than runtime performance.

What exactly are those developers doing all day then? Because every time I open an Instagram.com link I'm struck by how bad it is. The images are small, the comment list is the same height as the image, so wide but short images have short comment list, the comments are scrolled to the bottom so the poster's description isn't visible without scrolling up, videos doesn't have any kind of playback controls so you can't…

Really? You’re dogging the product because you don’t like how it looks on mobile? The article is about how they use python and types to improve their APIs.

Re: Types for Python HTTP APIs

#40

Earlier quoted context omitted.

Yeah I feel FastAPI will become the new Flask in a few years. What else does it offers besides performance (switching to async is no free lunch)?

I ran across FastAPI earlier this year and did a tiny prototype to play with it. Selling points for me: - Integrates nicely with some existing libraries (Starlette, Pydantic) - Well documented - Auto-validation of endpoints from data models - Auto-generation of OpenAPI schemas from those models - Auto-serves live API docs from that schema - Easy definition of sync and async endpoints Again, it was just a tiny proof-o…

> - Auto-generation of OpenAPI schemas from those models

Is there a package that can do a reverse? Can I give it an OpenAPI spec and get a code stump going?

Post reply on HN