How elixir better in concurrency than python?

How elixir better in concurrency than python?

Elixir is built on top of the BEAM (erlang virtual machine) which is built around the concept of mass concurrency through very lightweight processes that communicates by sending messages.

These processes have their own mailbox, heap, memory and garbage collection which means that they can both work and fail independently. The beam will then use a set of schedulers that decides which process get to do work and which processor core it will utilize for that work.
In short this will enable (among a lot of other good/cool things) a system that is concurrent and parallel in nature.

I’m not at all a python developer but I believe python achieves concurrency through threads which share memory with the rest of the system. In such a setup it’s for example important to not have one part of the program clean up memory that another thread might be using so they have something called a GIL (Global Interpretation Lock) which will control the system on a global level and make sure code don’t crash in ways a developer cannot prevent. This can mean that running code in parallel becomes very hard without using a separate isolated runtime which comes with extra resources costs.

With that said I think python could very well handle concurrency as good as elixir in many use-cases but it would be much easier to achieve the same output in elixir.

Hope it gives you some answers :slight_smile:

2 Likes

The model of concurrency used by elixir shines in medium to big applications, in case of very big applications you have to take a step back and to use something like semaphores, I think this talk covers the situation where processes outlive their usefulness.

But in general you are right, why use some primitive forms of concurrency where you have to handle everything including locking of resources, when you can use elixir and be a efficient and have concurrent safe code.

1 Like

Two main reasons:

  • Python has a shared memory space for all threads, and so the main C implementation needs a lock (the “global interpreter lock”) to prevent multiple threads from modifying things simultaneously. More info at the Python wiki page

  • Python’s standard implementation of threads uses operating-system threads which are expensive to start and expensive to switch between. If you start a couple thousand at once, you’re Gonna Have A Bad Time.

The BEAM that powers Elixir has different answers to these questions:

  • BEAM processes can only communicate by sending messages, so there are no shared things and no need to lock them

  • BEAM processes are “lightweight”, similar to green threads in Python. A single machine can easily accommodate millions

2 Likes

Thanks @johantell very helpful