Easy Parallelism in Ruby
Last week, @voxdolo and I were faced with a problem: our application needed to make 3 API calls at once, and while two of the services were ok (finishing in three seconds or so), the third would take up to 8 seconds to retrieve it’s data. When done synchronously this meant on a good day we’d get out in 14 seconds, which is simply unacceptable.
After deciding we didn’t want to manage threads ourselves, we discovered the excellent Peach library. Peach, which stands for parallel each, is an insert and go parallelized each (via .peach) for arrays, not to mention map (.pmap) and select (.pselect). Simply replace the standard ruby method calls you need to be asynchronous with the peach equivalent, and you’re good to go.
In case you haven’t gotten it yet…
So while the name is only clever for the basic each, the lib is still awesome for quickly tackling async actions.
With a little UX love and the now 8 second data retrieval, we were able to make this workflow intuitive to the user (and less painful in development). Huzzah.
One caveat: In using this with MRI, you will see a slight decrease in speed of an individual process, however in our case, the gains have been dramatic.
blog comments powered by Disqus