Fork with spawn - RailsOnWave Ruby on Rails w...
来源:百度文库 编辑:神马文学网 时间:2024/07/04 11:35:52
Fork with spawn
Posted by Sandro Paganotti inRuby on Rails - comments are closed![](http://image2.360doc.cn/DownloadImg/2008/11/24/11192_1991679_1.gif)
![](http://image2.360doc.cn/DownloadImg/2008/11/24/11192_1991679_2.jpg)
During the development of our current project we needed to handle a very long task (4 minutes). After a bit of googling i foundspawn by Tom Anderson. Here’s how it works:
Spawn let you fork (or thread) a block of code by simply pass it as a parameter to a ‘spawn’ function:
spawn do# very long long taskend
what spawn do (when used with ‘fork’) is recreate an ActiveRecord Connection inside afork method and then execute your block of code. You can also set the priority of the process being create with the option ‘nice’:
spawn(:nice => 7) do# do somethingend
Finally you can wait the end or your child processes with the method Spawn::wait() (I took this example from the official readme):
N.times do |i|# spawn N blocks of codespawn_ids[i] = spawn dosomething(i)endend# wait for all N blocks of code to finish runningwait(spawn_ids)
I haven’t tried to use spawn with threads but as far as I’ve tested it with fork, it works perfectly.
Posted by Sandro Paganotti inRuby on Rails - comments are closed
![](http://image2.360doc.cn/DownloadImg/2008/11/24/11192_1991679_1.gif)
![](http://image2.360doc.cn/DownloadImg/2008/11/24/11192_1991679_2.jpg)
During the development of our current project we needed to handle a very long task (4 minutes). After a bit of googling i foundspawn by Tom Anderson. Here’s how it works:
Spawn let you fork (or thread) a block of code by simply pass it as a parameter to a ‘spawn’ function:
spawn do# very long long taskend
what spawn do (when used with ‘fork’) is recreate an ActiveRecord Connection inside afork method and then execute your block of code. You can also set the priority of the process being create with the option ‘nice’:
spawn(:nice => 7) do# do somethingend
Finally you can wait the end or your child processes with the method Spawn::wait() (I took this example from the official readme):
N.times do |i|# spawn N blocks of codespawn_ids[i] = spawn dosomething(i)endend# wait for all N blocks of code to finish runningwait(spawn_ids)
I haven’t tried to use spawn with threads but as far as I’ve tested it with fork, it works perfectly.
Fork with spawn - RailsOnWave Ruby on Rails w...
ONLamp.com -- Rolling with Ruby on Rails
Integrate FCKEditor with your Ruby on Rails application
Integrate FCKEditor with your Ruby on Rails application
什么是Ruby on Rails
Ruby on Rails
什么是Ruby on Rails
Ruby on Rails Cheat Sheet - Ruby On Rails - ILoveJackDaniels.com
[Ruby on Rails]Vim 开发 ruby on rails 完全配置
What Is Ruby on Rails
Ruby on Rails 学习资料
Ruby on Rails 学习资料
Vim 开发 ruby on rails 完全配置...
Ruby on Rails有用的插件
Vim 开发 ruby on rails 完全配置...
通过JavaEye2.0网站看ruby on rails性能-rails-Ruby -Ja...
在Linux平台上安装和配置Ruby on Rails详解-rails-Ruby
ruby on rails:web2.0世界新生的创造力
ruby on rails:web2.0世界新生的创造力
RoR历险记:一个Java程序员的Ruby on Rails体验
RUBY ON RAILS:WEB2.0世界新生的创造力
利用Radrails开发Ruby on Rails程序入门指南
修改默认主页---Ruby on Rails:Welcome aboard
Ruby On Rails 项目时间小结-yueguangyuan -JavaEye技术社...