开发者

Most simple way to do something after page was generated and sent to the user

开发者 https://www.devze.com 2023-04-03 22:26 出处:网络
I need to do some image manipulation (that could take long time) after page was generated and sent to the user. Not to make user wait while this job is done. Somethi开发者_运维百科ng like this:

I need to do some image manipulation (that could take long time) after page was generated and sent to the user. Not to make user wait while this job is done. Somethi开发者_运维百科ng like this:

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

I would like to avoid daemonizing.


Delayed Job would probably be the easiest thing:

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

Then the actual do_image_manipilation call will be dealt with later. There is a daemon of sorts involved (just a constantly running rake task) but you don't have to deal with the details yourself, you just stick a .delay in the right places and Delayed Job takes care of the heavy lifting.


You could try using threads. Just render your view, and then spawn a new thread.


Not sure that this can be done without running some kind of processing daemon. Have you looked at beanstalkd or resque?

0

精彩评论

暂无评论...
验证码 换一张
取 消