Log in

Registration

Multi-Processing with Ruby

Posted: December 21, 2010 / in: Scripts / No comments

ruby-logo-160 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.

  • Web Servers
  • Email Servers
  • Kernel drivers / modules (…to handle requests to file systems, memory areas or hardware devices)

…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…

The Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Source: www.linux-support.com
Author: Mario Scondo
Date: 2010-12-21
 
 
# Contents:
# 1. Multi-Processing
#
"""
 
include Process
 
WAIT_FOR_PID = 0
 
def mymethod(variable)
sleep 1
print '...uhh!'
return variable
end
 
puts "Application started."
 
pid1 = fork { sleep 0.2; print "1"; print "2"; print "3" }
pid2 = fork { print "a"; print "b"; sleep 0.4; print "c" }
pid3 = fork { mymethod('sabber') }
Process.waitpid(pid1, WAIT_FOR_PID) # wait for process 1
Process.waitpid(pid2, WAIT_FOR_PID) # wait for process 2
Process.waitpid(pid3, WAIT_FOR_PID) # wait for process 3
 
puts
puts "Application stopped."
 

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!

Output

When starting the script you will receive the following outputs:

Application started.
ab123c...uhh!
Application stopped.
 

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!

Cross-Platform Issue

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.

 

Related articles:

Related resources:

Incoming search terms:

© Copyrights and Licenses, 2014 - Linux-Support.com The Professional Linux and OSS Services Portal