Skip to content

Commit

Permalink
added :nice option
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/spawn/spawn@19 2aabfe31-cc40-457c-aec7-21ad9ce7fb7d
  • Loading branch information
tra committed May 11, 2008
1 parent a3aa889 commit c525078
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ v0.7 - 2008/04/24
v0.8 - 2008/05/02
* call exit! within the ensure block so that at_exit handlers aren't called on exceptions
* set logger from RAILS_DEFAULT_LOGGER if available, else STDERR

--------------------------------------------------
v0.9 - 2008/05/11
* added ability to set nice level for child process
11 changes: 9 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ spawn to Spawn::wait(), like this:
# wait for all N blocks of code to finish running
wait(spawn_ids)

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
do_something_nicely
end

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.
Expand Down Expand Up @@ -104,7 +111,7 @@ blog post on threading in rails.
http://bibwild.wordpress.com/2007/08/28/threading-in-rails/

Also thanks to all who have helped debug problems and suggest improvements including:
Ahmed Adam, Tristan Schneiter, Scott Haug, Andrew Garfield, Eugene Otto, <your name here>

Ahmed Adam, Tristan Schneiter, Scott Haug, Andrew Garfield, Eugene Otto, Dan Sharp,
<your name here>

Copyright (c) 2007-08 Tom Anderson (tom@squeat.com), see LICENSE
6 changes: 6 additions & 0 deletions lib/spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ def fork_it(options)
begin
start = Time.now
@@logger.debug "spawn> child PID = #{Process.pid}"

# set the nice priority if needed
Process.setpriority(Process::PRIO_PROCESS, 0, options[:nice]) if options[:nice]

# disconnect from the listening socket, et al
Spawn.close_resources
# get a new connection so the parent can keep the original one
ActiveRecord::Base.spawn_reconnect

# run the block of code that takes so long
yield

rescue => ex
@@logger.error "spawn> Exception in child[#{Process.pid}] - #{ex.class}: #{ex.message}"
ensure
Expand Down

0 comments on commit c525078

Please sign in to comment.