This article illustrates how to utilize Ruby’s features to perform parallel processing.
One way to perform processing in parallel is to utilize multiple processes. If you are asking yourself when multi-processing is required, then take a look at the following list of applications that are utilizing this technique (or similar approaches) to handle many concurrent requests.
…and many, many more.
However, the following script is utilizing the Process interface of Ruby to start three additional processes in parallel. Just take a look at it and try to find out what is happening…
Line 24, 25: The first two processes are started by defining two blocks that are performing some outputs.
Line 26: This is an example how to start a process by invoking a previously defined method.
All three processes are consisting of some statements to print strings and to fall to sleep for some time.
There are a number of additional features related to the threading interface. Please take a look at the following manual to get an idea what is possible with Ruby. It contains a description and a lot of examples how to interface with processes!
When starting the script you will receive the following outputs:
Please note: the previous outputs may vary when executed on other machines. Because of the nature of program code working in parallel the outputs are a bit messy when no synchronization or queuing is performed!
Remarkably the Module Process ist not available for all mature platforms. E.g. we did try to utilize fork() at MS Windows based installations. Ruby 1.8 and 1.9 did complain that ‘fork() function is unimplemented on this machine (NotImplementedError)‘.
If you are interested in cross-platform compatibility and in doing parallel computing you have to utilize the threading interface of Ruby. Python for example, provides an internal fallback mechanism to utilize the libraries that are available at the local platform.