Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Ruby or Python web sites to use multiple cores?

Even though Python and Ruby have one kernel thread per interpreter thread, they have a global interpreter lock (GIL) that is used to protect potentially shared data structures, so this inhibits multi-processor execution. Even though the portions in those languajes that are written in C or C++ can be free-threaded, that's not possible with pure interpreted code unless you use multiple processes. What's the best way to achieve this? Using FastCGI? Creating a cluster or a farm of virtualized servers? Using their Java equivalents, JRuby and Jython?

like image 620
Miriam Ruiz Avatar asked Aug 31 '08 21:08

Miriam Ruiz


People also ask

Can Python threads run on multiple cores?

Key Takeaways. Python is NOT a single-threaded language. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.

How many CPUs or cores will the Python threading library take advantage of simultaneously?

How many CPUs (or cores) will the Python threading library take advantage of simultaneously? Python threading is restricted to a single CPU at one time. The multiprocessing library will allow you to run code on different processors.

How many CPUs will the threading library use?

How many CPUs will the threading library use? 1. Zero. threading does not make use of CPUs.

Is Ruby single threaded?

The Ruby Interpreter is single threaded, which is to say that several of its methods are not thread safe. In the Rails world, this single-thread has mostly been pushed to the server.


2 Answers

I'm not totally sure which problem you want so solve, but if you deploy your python/django application via an apache prefork MPM using mod_python apache will start several worker processes for handling different requests.

If one request needs so much resources, that you want to use multiple cores have a look at pyprocessing. But I don't think that would be wise.

like image 167
Peter Hoffmann Avatar answered Sep 29 '22 02:09

Peter Hoffmann


The 'standard' way to do this with rails is to run a "pack" of Mongrel instances (ie: 4 copies of the rails application) and then use apache or nginx or some other piece of software to sit in front of them and act as a load balancer.

This is probably how it's done with other ruby frameworks such as merb etc, but I haven't used those personally.

The OS will take care of running each mongrel on it's own CPU.

If you install mod_rails aka phusion passenger it will start and stop multiple copies of the rails process for you as well, so it will end up spreading the load across multiple CPUs/cores in a similar way.

like image 24
Orion Edwards Avatar answered Sep 29 '22 02:09

Orion Edwards