8 ms·
Sanic – Python 3.5+ web server that's written to go fast
- shakna 10y agoAsync handlers, Flask-style API, and fast. Disclaimer: I have nothing to do with this project.
- rgacote 10y agoLooks like a nice step up from the raw aiohttp I've been writing. Would be interesting to see this wrapped with a forking module. That would provide an nginx-style deployment--select the number of actual threads to run and then run async within each thread.
- aitoehigie 10y agoLink to the docs?
- shakna 10y agoRight there in the README, but here's the Getting Started [0] anyway. [0] https://github.com/channelcat/sanic/blob/master/docs/getting_started.md https://github.com/channelcat/sanic/blob/master/docs/getting...
- __s 10y agoCurious how much this benefits from optimizations coming in Python 3.6. Is the benchmark essentially spending all the time in uvloop Cython code?
- benbristow 10y agoGotta go fast.
- mangeletti 10y agoGotta go faster.
- Fluid_Mechanics 10y ago[Air Horn]
- sixhobbits 10y agoThe comparisons are nice, but it would have been nice to see a comparison with Flask + Nginx/Apache2 for example.
- rcarmo 10y agoCurious to see how this fares against something like uWSGI with aiohttp support (haven't tried either, but uWSGI brings so much more to the table than speed that I'd like to be able to make an informed choice).
- ddorian43 10y agoasyncio is experimental still on uwsgi http://uwsgi-docs.readthedocs.io/en/latest/asyncio.html http://uwsgi-docs.readthedocs.io/en/latest/asyncio.html
- rcarmo 10y agoWhich is why I haven't tried it yet. :)
- dom0 10y agoRunning asyncio-based servers on uwsgi is very stable though, but may not achieve 100 % of the performance native asyncio in uWSGI may have. Even then, Tornado is "fast enough" for us (ie. we run out of network bandwidth before we run out of CPU). But the main advantage - that you're still using uwsgi as a "unified application server" of sorts - persists, which was the main point for us (Django, Flask, Tornado on uWSGI). This simplified deployment and administration quite a bit. Re. topic: It would be really interesting to see it compared to Tornado.
- rcarmo 10y agoYeah, I use uWSGI for a lot of things (check out http://github.com/rcarmo/piku http://github.com/rcarmo/piku, for instance), so am more interested in figuring out if I can get a little extra mileage off it.
- okso 10y agoNo support for websockets ?
- tschellenbach 10y agoI think it's fine to use an external library such as Faye for websockets. That comes with the benefit of having many client libraries available. (JS, iOS, Android)
- revelation 10y agoWhen did the principal benefit of Websockets (send and receive whatever instead of HTTP primitives) become a matter of client and server library availability? I want to trace this profound moment in WebDev history. Amazing.
- yunruse 10y agoThe principle of sending whatever is important, but in reality most modern consumption literally follows the ethos of 'go fast'. Perhaps dropping Websockets helps out; it's a freedom of choice to pick speed over compatibility.
- tschellenbach 10y agoReally excited about these efforts. Python is my main programming language, but for high volume endpoints it's sometimes necessary to switch to other languages. In an ideal world I'd just use Python for everything. It will take a while before that's possible though. These performance gains don't mean much if your DB connection is still blocking. How does your benchmark compare against GO, Elixir and Node?
- napperjabber 10y agoMagicStack has already taken the initiative. https://github.com/MagicStack/asyncpg https://github.com/MagicStack/asyncpg
- spamizbad 10y agoStill beta, but those postgres numbers are incredible. MagicStack is doing amazing things.
- mangeletti 10y agoExtrapolating from Techempower and from the OP's benchmarks, I got: 137% as many req/sec as Node 83% as many req/sec as Go I'm it sure where this goes when you factor in async IO, but my guess is that Node gains a little and Go loses a lot. When you add asyncpg to the mix my money is on Sanic winning by a long shot, in a benchmark that includes e.g., 3 queries and returning the results as JSON.
- mhuffman 10y agoI have found node and tornado to have practically identical performance. I have not had a lot of experience with GO and Elixir for web servers. nginx (openresty, really) has beat both of them them in all "normal" (ie. not limited by DB access, etc) usage conditions that I have tested.
- spamizbad 10y agoThis is a great project. I am going to be watching this! One question: Why default to ujson? I understand that if you're always dealing with small, simple json objects it's quite fast and safe. But with larger json payloads, its performance and compatibility start to break down. I haven't touched it in the last 8 months, but I had to switch to Python's built-in json module (which is quite fast in 3.5) for compatibility when serializing and deserializing large json objects.
- agf 10y agoDo you have a good writeup of this problem? I've seen only scattered, anecdotal info about it.
- saynsedit 10y agoI've actually written a highly optimized asyncio-based Python web server in the past. I meticulously optimized every component, using all the standard CPython optimization techniques (heavy use of stdlib, minimizing method lookups). In the end, my implementation was nowhere near nginx. I even ran it under PyPy and it fared no better. Then I realized the oxymoronic nature of writing an optimized web server in Python.
- dr_zoidberg 10y agoIf you wanted to run in PyPy, you should've written your code to be PyPy-friendly from the beginning. Standard CPython optimizations don't usually fare well in PyPy, and you can end up with even slower speed if you follow those. Still, you didn't mention profiling the hotspots of your server, nor using Cython to optimize them. I don't see how writing an optimzed web server in Python becomes oxymoronic -- it can still be the fastest performing python server, and have a valid use case for those that want to work in Python.
- saynsedit 10y agoYour PyPy point is fair. A PyPy-oriented optimization effort could have improved performance. N/s by what magnitude though. I did extensive benchmarking until there were no more hot spots, didn't help. Dropping into C or Cython was a non-goal. For dev an embedded Python web server is convenient but it doesn't have to be fast. When it comes to performance, it always makes more sense to use a native-code web server in production.
- yahyaheee 10y agoThis looks awesome, exactly what python needs right now
- lbolla 10y agoWhat's up with those latency numbers? Seem huge to me.
- sitkack 10y agoWho measures average latency? Nearly meaningless.
- shincert 10y agoI'm curious. Why is it meaningless? What would you measure?
- sitkack 10y agoYou almost never want to use average as your metric when dealing with time. For time, the 95 or 99 percentile latency is going to be measure of how many requests out of a hundred a less than your metric. Esp when the thing you are measuring is a piece of larger system, which is the crux, because of how a resource in turn requests k-more resources. Each request has an equal chance of being over the latency percentile. Even at 10 requests, only 90% of sessions will be under the 99% latency. See also, Gil Tene https://www.youtube.com/watch?v=lJ8ydIuPFeU https://www.youtube.com/watch?v=lJ8ydIuPFeU A nice overview, http://bravenewgeek.com/everything-you-know-about-latency-is-wrong/ http://bravenewgeek.com/everything-you-know-about-latency-is... See also, http://highscalability.com/blog/2015/10/5/your-load-generator-is-probably-lying-to-you-take-the-red-pi.html http://highscalability.com/blog/2015/10/5/your-load-generato...
- adamnemecek 10y agoBest name ever.
- leemalmac 10y agoThis is awesome, Python is my number one, Node.js already has much larger ecosystem for async i/o. If we need something faster for heavy computations Go is a good choice. But maybe in near future Python will grow significantly, who knows.
- yakcyll 10y agoWhy it is app.run() and not app.go_fast() is beyond me.
- btmiller 10y agoGood point. With Sanic you gotta .go_fast()
- asdfologist 10y agoI noticed that the past several Python-related threads on HN haven't contained the usual cynical comments against adopting Python 3. Are we finally at a point where Python 3 has been accepted as the gold standard?
- tobltobs 10y agoBy my gut feeling this happened this summer. I haven't found any project which wasn't available in Python 3 in the last three months and therefore stumbled upon some which don't support 2 anymore. Also newer answers on SO now usually start with the version for 3 and might have an appendix "if you are still on ...".
- oliwarner 10y agoYes. I think the async syntax in 3.5 was the tipping point for the stragglers. It finally looked like a serious upgrade, not just a bigger number with a load of things to fix. I know there are people who are stuck on old versions, but the community has recognised how much damage this dispute was doing to Python's image. They know they'll have official Cpython support to 2020, so it's a non-issue until then. Oh and Ubuntu 16.04 defaulting to 3.5 helped too.
- dom0 10y agoI've mostly heard this from people who don't use/know Python in the last ~2 years. Imho this has been a trope for some time: PHP is such a bad language hue hue Java. AbstractCommentPostingRequestSerializerInterface hue hue Python. You know Python 3 right? hue hue hue command.exe windows suxxx hue hue ... countless others with varying stupidity
- floatboth 10y agoI love the name!! :D The hard dependency on uvloop looks weird. It's just a pluggable loop for asyncio, anyone can switch to it in two lines of code, no reason to depend on something that's a native extension. Also why not just improve aiohttp's performance…
- nitely 10y ago> Also why not just improve aiohttp's performance… Have you read aiohttp's code? there have been some discussions about improving its performance, there is basically no immediate bottleneck, the whole thing just performs poorly. I hate to say it but sometimes it's just better to start from scratch.
- magicbuzz 10y agoI honestly don't understand why you would do this when nginx with Luajit is so fast. I think Python is great but nginx is so robust, widely used and Luajit is so impressively quick.
- petre 10y agoProbably because even though Lua is super a nice language, writing code in a config file is not that much fun? App servers spitting out HTML written in the language of your choice are so much more flexible than developing on a single platform/stack such as Apache + mod_php|mod_perl, or OpenResty. Of course nothing stops you to proxy them through Nginx and serve the static content directly with Nginx in production.
- SwellJoe 10y agoIt's interesting how so many of these microframework+app server type things spread across many languages have converged on a very, very, similar set of conventions and practices. The example code looks strikingly like every other recent JavaScript/Perl/Python/Ruby microframework+server that I've tinkered with lately (Express, Koa, and Hapi in JS, Mojolicious in Perl, and Flask in Python). Aside from the obvious language differences, the concepts are all so similar. I wonder if this is a convergence on a local maximum for writing web apps, or if it's just a situation where the most popular ones did it this way, so everyone does it this way. Not that it matters too much, in the general case. The route and app setup plumbing is a small part of most applications, so it's rarely going to be ruinous if you spend an extra hour or two getting it working right. But, since I've been evaluating a bunch of different ways to build web app backends lately, I've just noticed how similar they all are, no matter what language you choose. Most of the ones I've been looking at are asynchronous, so this also has that similarity to most of the others. While one could argue that it's obvious that they would look the same...but, web development didn't always look this way. The first 10-15 years of web application development I did looked very different, in fact (CGI or mod_perl or PHP, which has some quite different conventions).
- minitech 10y agoThe README suggests this one is written to be Flask-like, and Koa is based on Express/Connect.
- MildlySerious 10y agoAnd Express as well as many others are loosely based on what Rails did iirc
- minitech 10y agoI don’t see how Express is at all based on Rails. They both have… similar URL pattern syntax that Rails didn’t invent?
- mirekrusin 10y ago
- cpeterso 10y agoTIL about "Sanic Hegehog": http://knowyourmeme.com/memes/sanic-hegehog http://knowyourmeme.com/memes/sanic-hegehog
- infocollector 10y agoCan we just use the webserver of Sanic with Flask on linux?