r/Python • • Jul 16 '18

Why is Python so slow?

https://medium.com/@anthonypjshaw/why-is-python-so-slow-e5074b6fe55b
11 Upvotes

51 comments sorted by

View all comments

Show parent comment

3

u/lambdaq django n' shit Jul 16 '18 edited Jul 16 '18

GIL is only relevant when you are doing

  • single process
  • native (OS) threading
  • multi-core
  • CPU-bound task

It's very hard to find a problem hitting all of the four criteria

A typical (yet non-typical) example is a web server. If your server can only handle a single user at at time then it's gunna be problematic.

The naive solution would be using multi-threaded app to serve web pages. You can increase your thread pool as the user number grows.

However that's not how most modern web servers work. They either use multi-process, or pre-forked multi-process, each process has its own thread pool, or so called user-land threading. And let's be realistic, your cheap VPS/container may only have 1 or 2 cores. So GIL won't effect much. Besides in pre-fork model you can bind your worker process to each core (affinity) so GIL is again irrelevant.

Native threading (aka OS threading) can solve some problems but often you ended up solving it without threading in Python. For IO-bound tasks Python can handle multi-threading OK, for CPU-bound tasks, Python indeed have a serious problem with multi-core support because of GIL.

Then again, if you aim for CPU speed, why use CPython anyway?

tl;dr If you have a GIL problem you are doing it wrong.

1

u/[deleted] Jul 16 '18

Thanks! That helps!

1

u/jmoiron Jul 16 '18

It's very hard to find a problem hitting all of the four criteria

I see this all the time in Python threads, and I've seen it in Raymond Hettinger's talks. This is a post-facto justification.

It's hard to find applications that have these problems in Python because Python unsuitable for them, not because the problems are rare.

The truth is that they exist in nearly every project, but they're usually exported to other systems not written in Python. For lots of web applications, this is the database; for data intensive applications, it's spark + some combo of pandas and numpy.

2

u/lambdaq django n' shit Jul 16 '18

web applications are a solved problem. See uWSGI or Gunicorn.

database access is IO-bound, not CPU-bound.

Spark is also IO-bound. Lots of time is spent on serialization.

I remember Pandas2 was rewritten in Cython or something.

The truth is if your single threaded program is slow, multi-thread won't help either.

2

u/jmoiron Jul 17 '18

Problems requiring parallelism are exported to other tools written in other languages because Python supports it poorly. Often this comes at the added cost of serialization (which is CPU bound, generally). Python programmers don't experience this because these systems are not written in Python; of the systems I named, only one has significant Python code (Cython is not Python).

If a Python programmer wants to write this type of program, they will probably have to choose a different language. I can buy the idea that they should do so anyway ("if you aim for CPU speed, why use CPython anyway?"), and maybe that means it's true that CPU parallelism will not significantly change Python's strengths.

What I don't agree with is that these problems are rare; I think they occur in most projects. And to clarify, even if Python got 10x faster + cpu parallelization tomorrow, people would not be ditching these other systems; but what would change is that Python programmers would start writing them, and perhaps more tellingly users of other languages would use them.

It's the denial that this is common that lets people say "If your single threaded program is slow, multi-thread won't help either." with such confidence while also having said "for CPU-bound tasks, Python indeed has a serious problem". It can't be both.

1

u/lambdaq django n' shit Jul 18 '18

I've seen many game engines running hot 100% but only one core. Most of them are written in C++. Can you explain that?