2011-09-08 39 views
1

我需要在生成页面并将其发送给用户后执行一些图像操作(可能需要很长时间)。这项工作完成后,不要让用户等待。就像这样:生成页面后最简单的方式生成并发送给用户

if @post.save! 
    redirect_to :action => :index 
    # Now user is redirected and don't need to wait 
    # doing job in a background 
    do_image_manipulation 
end 

我想避免daemonizing。

回答

1

你可以尝试使用线程。只需渲染你的视图,然后产生一个新的线程。

1

不确定这可以在没有运行某种处理守护进程的情况下完成。你看过beanstalkd还是resque

2

Delayed Job很可能是最简单的事情:

if @post.save! 
    @post.delay.do_image_manipulation 
    redirect_to :action => :index 
end 

那么实际do_image_manipilation通话将被处理后。有一个涉及到的恶魔(只是一个持续运行的耙子任务),但你不必自己处理细节,只需在适当的地方粘上一个.delay,Delayed Job负责繁重的工作。