Live data from Hacker News

Async Python is not faster

calpaterson.com

331–340 of 364 posts

Re: Async Python is not faster

#331

Earlier quoted context omitted.

The database is the bottleneck. JIT or even C++ shouldn't even be a factor here. Something is wrong with the python implimentation of async await.

If I/O-bound tasks are the problem, that would tend to indicate an issue with I/O event loop, not with Python and its async/await implementation. If the default asyncio.SelectorEventLoop is too slow for you, you can subclass asyncio.AbstractEventLoop and implement your own, such as buildiong one on top of uvloop. And somebody's already done that: https://github.com/MagicStack/uvloop Moreover, even if there's _still_…

When I talk about async await I'm talking about everything that encompasses supporting that syntax. This includes the I/O event loop.

So really we're in agreement. You're talking about reimplementing python specific things to make it more performant, and that is exactly another way of saying that the problem is python specific.

Re: Async Python is not faster

#332
post #197

Earlier quoted context omitted.

I see this elitist attitude all over the internet. First it was people saying “Guys why are you over reacting to corona the flu is worse.” Then it was people saying “Guys, stop buying surgical masks, The science says they don’t work it’s like putting a rag over your mouth.” All of these so called expert know it alls were wrong and now we have another expert on asynchronous python telling us he knows better and he’s n…

> Nodejs blew everything out of the water Node's JIT comes from a web browser's javascript implementation used by billions of people. It's also had async baked in from day one. Python started single process, added threading, and then bolted async on top of that. And CPython is a pretty straight interpreter. A comparison between Node and PyPy would be more informative, but PyPy has a far less mature JIT and still has…

Except IO is the bottleneck here. The concurrency model for IO should determine overall speed. If python async is slower for IO tasks then sync then that IS an unexpected result and an indication of a python specific problem.

Re: Async Python is not faster

#333

Earlier quoted context omitted.

I am sorry to be blunt, but that sounds like a PR statement filled with nonsense. > If I'm writing a generic database-backed web app If you are writing a system where performance does not matter, then performance does not matter. > a machine learning system or a video game. Most of those, when written in C, are finished once they work, or at the very most have some very basic, minimal profiling / optimization. Wait,…

> If you are writing a system where performance does not matter, then performance does not matter. It's not binary. Performance always matters, but there are different levels of value to that performance. Writing hand-tweaked assembly code is rarely a good point on the ROI curve. > Wait, what? ML engine backends and high-level descriptions, and video games are some of the most heavily tuned and optimized systems in e…

> And the major language most machine learning researchers use is Python.

Read again what I wrote. Even the model itself is optimized. The fact that it is written in Python or in any DSL is irrelevant.

> I used to think that too before I spent years doing functional programming.

I have done functional programming in many languages, ranging from lambda calculus itself to OCaml to Haskell, including inside and outside academia. It does not change anything I have said.

Perhaps you spent way too many years in high-level languages that you have started believing magical properties about their compilers.

> prided myself on being able to implement things like highly-optimized numerical code with templates.

Optimizing numerical code has little to do with code monomorphization.

It does sound like you were abusing C++ thinking you were "optimizing" code without actually having a clue.

Like in the previous point, it seemed you attributed magical properties to C++ compilers back then, and now you do the same with high-level ones.

> It actually took a few years before my code in Lisp, Scheme, JavaScript, and Python stopped being structured like C++.

How do you even manage write code in Lisp etc. "like C++"? What does that even mean?

> You putting "Python" and "Java" in the same sentence shows this isn't a process you've gone through yet. Java has roughly the same limitations as C and C++.

Pure nonsense. Java is nowhere close to C or C++.

> Here's a challenge for you.

I would use Mathematica or Julia for that. Not Scheme, not C++. Particularly since you already declared the last 30% of performance is irrelevant.

You are again mixing up domins. You are picking a high-level domain and then complaining a low-level tool does not fit nicely. That has nothing to do with the discussion and we could apply that flawed logic to back any statement we want.

Re: Async Python is not faster

#334

Earlier quoted context omitted.

> It took until we had bytecode+JIT that performance roughly lined up. It really didn't. Yes, in highly specialized benchmark situations, JITs sometimes manage to outperform AOT compilers, but not in the general case, where they usually lag significantly. I wrote a somewhat lengthy piece about this, Jitterdämmerung : https://blog.metaobject.com/2015/10/jitterdammerung.html Discussed at the time: https://news.ycombina…

Well, if you wanna go that route, in the general case, code will be structured differently. On one side, you have duck typing, closures, automated memory management, and the ability to dynamically modify code. On the other side, you don't. That linguistic flexibility often leads to big-O level improvements in performance which aren't well-captured in microscopic benchmarks. If the question is whether GC will beat mal…

Objective-C has duck typing (if you want), closures, automated memory management and the ability to dynamically modify code.

And is AOT compiled.

GC can only "beat" malloc/free if it has several times the memory available, and usually also only if the malloc/free code is hopelessly naive.

And you've got the micro-benchmark / real-world thing backward: it is JITs that sometimes do really well on microbenchmarks but invariably perform markedly worse in the real world. I talk about this at length in my article (see above).

Re: Async Python is not faster

#335

Earlier quoted context omitted.

> If you are writing a system where performance does not matter, then performance does not matter. It's not binary. Performance always matters, but there are different levels of value to that performance. Writing hand-tweaked assembly code is rarely a good point on the ROI curve. > Wait, what? ML engine backends and high-level descriptions, and video games are some of the most heavily tuned and optimized systems in e…

> And the major language most machine learning researchers use is Python. Read again what I wrote. Even the model itself is optimized. The fact that it is written in Python or in any DSL is irrelevant. > I used to think that too before I spent years doing functional programming. I have done functional programming in many languages, ranging from lambda calculus itself to OCaml to Haskell, including inside and outside…

> Perhaps you spent way too many years in high-level languages that you have started believing magical properties about their compilers.

> It does sound like you were abusing C++ thinking you were "optimizing" code without actually having a clue.

> Like in the previous point, it seemed you attributed magical properties to C++ compilers back then, and now you do the same with high-level ones.

I think at this point, I'm checking out. You're making a lot of statements and assumptions about who I am, what my background is, what I know, and so on. I neither have the time nor the inclination to debunk them. You don't know me.

When you make it personal and start insulting people, that's a good sign you've lost the technical argument. Technical errors in your posts highlight that too.

If you do want to have a little bit of fun, though, you should look up the template-based linear algebra libraries of the late nineties and early 00's. They were pretty clever, and for a while, were leading in the benchmarks. They would generate code, at compile time, optimized to the size of your vectors and matrixes, unroll loops, and similar. They seem pretty well-aligned to your background. I think you'll appreciate them.

Re: Async Python is not faster

#336

Earlier quoted context omitted.

If I/O-bound tasks are the problem, that would tend to indicate an issue with I/O event loop, not with Python and its async/await implementation. If the default asyncio.SelectorEventLoop is too slow for you, you can subclass asyncio.AbstractEventLoop and implement your own, such as buildiong one on top of uvloop. And somebody's already done that: https://github.com/MagicStack/uvloop Moreover, even if there's _still_…

When I talk about async await I'm talking about everything that encompasses supporting that syntax. This includes the I/O event loop. So really we're in agreement. You're talking about reimplementing python specific things to make it more performant, and that is exactly another way of saying that the problem is python specific.

No, we're not in agreement. You're confounding a bunch of independent things, and that is what I object to.

It's neither fair nor correct to mush together CPython's async/await implementation with the implementation of asyncio.SelectorEventLoop. They are two different things and entirely independent of one another.

Moreover, it's neither fair nor correct to compare asyncio.SelectorEventLoop with the event loop of node.js, because the former is written in pure Python (with performance only tangentally in mind) whereas the latter is written in C (libuv). That's why I pointed you to uvloop, which is an implementation of asyncio.AbstractEventLoop built on top of libuv. If you want to even start with a comparison, you need to eliminate that confounding variable.

Finally, the implementation matters. node.js uses a JIT, while CPython does not, giving them _much_ different performance characteristics. If you want to eliminate that confounding variable, you need to use a Python implementation with a JIT, such as PyPy.

Do those two things, and then you'll be able to do a fair comparison between Python and node.js.

Re: Async Python is not faster

#337

I'm trying to figure out how to run these benchmarks on my own machine and experiment with some tweaks to the implementation, but it's unclear how to run these benchmarks from start to finish. I don't see any instructions for running the benchmarks in the github repository. @calpaterson can you provide guidance? I'd like to try an alternative query pattern. The current pattern implemented in the benchmarks is select…

Hi - you will need to pip install the requirements into a virtualenv. Then set $PWPWORKERS (eg to 1 to start with) and run serve-gunicorn-flask.sh. That will get you a gunicorn instance up and running. From them on you'll need to set up nginx, pgbouncer and postgres. I used unix sockets between all of these but using TCP/IP is fine. The data generation script is checked in, as is the schema.

Before you start you should know that Tudor M (see a PR on the project) experimented with changing the query patterns (to three queries, but not a count(*)). It doesn't change matters and the basic reason for that is that nothing has changed - simply having more blocking or non-blocking IO is irrelevant to throughput - except that the more yields you have the more problematic your response times are going to be under load.

Re: Async Python is not faster

#338

A lot of the debate and discussion here seems to come from the fact that the example program demonstrates concurrency across requests (each concurrent request is being handled by a different worker), but no concurrency within each request: The code to serve each request is essentially one straight line of execution, which pauses while it waits for a DB query to return. A more interesting example would be a request th…

This a great point, surprised you received no follow-up comments!

Re: Async Python is not faster

#339

Earlier quoted context omitted.

When I talk about async await I'm talking about everything that encompasses supporting that syntax. This includes the I/O event loop. So really we're in agreement. You're talking about reimplementing python specific things to make it more performant, and that is exactly another way of saying that the problem is python specific.

No, we're not in agreement. You're confounding a bunch of independent things, and that is what I object to. It's neither fair nor correct to mush together CPython's async/await implementation with the implementation of asyncio.SelectorEventLoop. They are two different things and entirely independent of one another. Moreover, it's neither fair nor correct to compare asyncio.SelectorEventLoop with the event loop of nod…

Except the problem here is that those tests were bottlenecked by IO. Whether you're testing C++, pypy, libuv, or whatever it doesn't matter.

All that matters is the concurrency model because that application he's running is barely doing anything else except IO and anything outside of IO becomes negligible because after enough requests, those sync worker processes will all be spending the majority of their time blocked by an IO request.

The basic essence of the original claim is that sync is not necessarily better than async for all cases of high IO tasks. I bring up node as a counter example because that async model IS Faster for THIS same case. And bringing up node is 100% relevant because IO is the bottleneck, so it doesn't really matter how much faster node is executing as IO should be taking most of the time.

Clearly and logically the async concurrency model is better for these types of tasks so IF tests indicate otherwise for PYTHON then there's something up with python specifically.

You're right, we are in disagreement. I didn't realize you completely failed to understand what's going on and felt the need to do an apples to apples comparison when such a comparison is not Needed at all.

Re: Async Python is not faster

#340

Earlier quoted context omitted.

No, we're not in agreement. You're confounding a bunch of independent things, and that is what I object to. It's neither fair nor correct to mush together CPython's async/await implementation with the implementation of asyncio.SelectorEventLoop. They are two different things and entirely independent of one another. Moreover, it's neither fair nor correct to compare asyncio.SelectorEventLoop with the event loop of nod…

Except the problem here is that those tests were bottlenecked by IO. Whether you're testing C++, pypy, libuv, or whatever it doesn't matter. All that matters is the concurrency model because that application he's running is barely doing anything else except IO and anything outside of IO becomes negligible because after enough requests, those sync worker processes will all be spending the majority of their time blocke…

No, I understand. I just think that your comparison with _node.js_ when there are a bunch of confounding variables is nonsense. Get rid of those and then we can look at why "nodejs will beat flask in this same exact benchmark".
Post reply on HN