views:

1025

answers:

5

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?

+1  A: 

Use an interface that runs each response in a separate interpreter, such as mod_wsgi for Python. This lets multi-threading be used without encountering the GIL.

EDIT: Apparently, mod_wsgi no longer supports multiple interpreters per process because idiots couldn't figure out how to properly implement extension modules. It still supports running requests in separate processes FastCGI-style, though, so that's apparently the current accepted solution.

John Millikin
+4  A: 

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.

Peter Hoffmann
+4  A: 

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.

Orion Edwards
+1  A: 

In Python and Ruby it is only possible to use multiple cores, is to spawn new (heavyweight) processes. The Java counterparts inherit the possibilities of the Java platform. You could imply use Java threads. That is for example a reason why sometimes (often) Java Application Server like Glassfish are used for Ruby on Rails applications.

dmeister
There's no need to spawn new processes, simply re-use them between requests.
John Millikin
yes, of course you can reuse a process for more than one request as you can use a threadpool to avoid spawning a thread per request. What I mean is that you have to spawn an own process per core that should be utilized.
dmeister
A: 

For Python, the PyProcessing project allows you to program with processes much like you would use threads. It is included in the standard library of the recently released 2.6 version as multiprocessing. The module has many features for establishing and controlling access to shared data structures (queues, pipes, etc.) and support for common idioms (i.e. managers and worker pools).

David Eyk