Thursday, 24 May 2012

Ruby vs Python: threads

Prior to Ruby 1.9, threads were implemented at green threads - threads were switched within the interpreter. In Ruby 1.9, threading is now performed by the operating system. This means that threads can now take advantage of multiple processors. However, there's major catch. Many Ruby extension libraries are not thread safe, so Ruby compromises: it uses native operating system threads but operates only a single thread at a time. You'll never see two threads in the same application running Ruby code truly concurrently. (You will, however, see threads busy doing, say I/O while another threads executes Ruby code. That's part of the point.)

This is also similar in Python. Due to the Global Interpreter Lock, in CPython only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). To make better use of the computational resources of multi-core machines, it is advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.

Reference: Programming Ruby 1.9: The Pragmatic Programmers' Guide (Facets of Ruby)

No comments :

Post a Comment