Background Process (threading and forking) with spawn plugin in rails

Do you have a process which takes a long amount of time before you display the result to the user? First of all make sure that your code is correct and you are following Rails standards. If thats alrite then you should do that process in background. Doing a process in background means that you start a asynchronous process as a separate process thread which keeps on running in background thus not interfering in user process. (Hope this clarifies what i mean by background process)

Now the question is how to do it and how its beneficial ??

Well by doing a background process you don’t keep a user waiting on your site. this helps the user to visit other parts of your site and since he gets faster responses he gets attached to your site and thats what you want being a website owner.

Now how to do it is the main thing. Well there are couple of plugins which claims to do this type of tasks.




But My Favourite is SPAWN plugin availabe on

Although the contents below are mentioned on the above link I m putting it here for your reference.


This plugin provides a 'spawn' method to easily fork OR thread
long-running sections of code so that your application can return
results to your users more quickly.This plugin works by creating new
database connections in ActiveRecord::Base for the spawned block.

The plugin also patches ActiveRecord::Base to handle some known
 bugs when using threads (see lib/patches.rb).


Here's a simple example of how to demonstrate the spawn plugin.
In one of your controllers, insert this code
(after installing the plugin of course):

  spawn do"I feel sleepy...")
    sleep 11"Time to wake up!")

If everything is working correctly, your controller should finish quickly
then you'll see the last log message several seconds later.

If you need to wait for the spawned processes/threads, then pass the
objects returned by spawn to Spawn::wait(), like this:

  N.times do |i|
    # spawn N blocks of code
    spawn_ids[i] = spawn do
  # wait for all N blocks of code to finish running

If you want your forked child to run at a lower priority than
the parent process, pass in the :nice option like this:

  spawn(:nice => 7) do

By default, spawn will use the fork to spawn child processes.
You can configure it to do threading either by telling the spawn
method when you call it or by configuring your environment.
For example, this is how you can tell spawn to use threading on the call,

  spawn(:method => :thread) do

When using the :thread setting, 
spawn will check to make sure that you have set
allow_concurrency=true in your configuration.   
If you want this setting then
put this line in one of your environment config files: 

  config.active_record.allow_concurrency = true

If it is not set, then spawn will raise an exception.

To (optionally) configure the spawn method in your configuration, 
add a line to your configuration file(s) like this:

  Spawn::method :thread

If you don't set any configuration, the :method will default to :fork.  To
specify different values for different environments, pass the environment as
the 2nd argument:

  Spawn::method :fork, 'production'
  Spawn::method :yield, 'test'

This allows you to set your production and development
environments to use different methods according to your needs.

Forking vs. Threading

There are several tradeoffs for using threading vs. forking.
Forking was chosen as the default primarily because it requires
no configuration to get it working out of the box.

Forking advantages:
  - more reliable? - the ActiveRecord code is generally not 
    deemed to be thread-safe.
    Even though spawn attempts to patch known problems with the
    threaded implementation, there are no guarantees.
    Forking is heavier but should be fairly reliable.
  - keep running - this could also be a disadvantage,
    but you may find you want to fork
    off a process that could have a life longer than its parent.
    For example, maybe you
    want to restart your server without killing the spawned processes.
    We don't necessarily condone this (i.e. haven't tried it)
    but it's technically possible.
  - easier - forking works out of the box with spawn,
    threading requires you set allow_concurrency=true.
    Also, beware of automatic reloading of classes in
    development mode (config.cache_classes = false).

Threading advantages:
  - less filling - threads take less resources...
    how much less?  it depends.
    Some flavors of Unix are pretty efficient at forking
    so the threading advantage may not be as big as you think...
    but then again, maybe it's more than you think.  ;-)
  - debugging - you can set breakpoints in your threads

I guess now its more clear to you what is Threading,Forking & bg procee
Be Sociable, Share!