Easy Ruby Paralellism With the 'Parallel' Gem
Concurrency is a hard topic in programming with many advanced concepts. We love libraries and solutions which help us during our everyday simple tasks. The ‘parallel’ gem is one of these - well, gems -. You can find it at
github
. The author’s own description:
Run any code in parallel Processes(> use all CPUs) or Threads(> speedup blocking operations). Best suited for map-reduce or e.g. parallel downloads/uploads.
Let’s see some simple example of its usage.
Sample code:
This code basically gets the root document of the http servers listening on port 80 on the list of domains, 10 times. The first run in a simple sequential run, we call this ’ normal ’ in the benchmark. Then we instruct the parallel library to use as many threads as our CPU core count is, this is the run called ’ thread ’ Finally we use forking (the default operation of the library), naturally this run is called ’ fork ’.
The results on my machine are:
I think this shows how easy and straightforward it is to use this library for everyday parallel tasks. Of course we haven’t covered advanced contexts as locks, mutexes, shared data, etc. - this library is not made for that.