Ruby 在sinatra中异步调用其他方法
我有以下的流程:Ruby 在sinatra中异步调用其他方法,ruby,sinatra,Ruby,Sinatra,我有以下的流程: upload big file and send token | V save uploaded file temporary | V response that file was correctly uploaded | V callback that checks is file is valid | V after validation make http request to defined address with to
upload big file and send token
|
V
save uploaded file temporary
|
V
response that file was correctly uploaded
|
V
callback that checks is file is valid
|
V
after validation make http request to defined address with token and
validation result, delete temporary file
在西纳特拉实施这一点的最佳方式是什么
我发现只有一种解决方案是使用如下内容:
还有其他方法可以处理这种情况吗?我使用的一种模式是后台线程池。请注意,我使用的是Web服务器puma,它可能无法与所有其他服务器一起工作 其思想是,在响应之前的上载路由中,将代码添加到一个持久性后台线程池中,该线程池将进行验证和http请求。我从宝石上取的线池 最简单的例子:
require 'sinatra/base'
require 'thread/pool'
class Threadexample < Sinatra::Application
class << self; attr_accessor :pool end # thread pool as class variable
@pool = Thread.pool(2)
post '/upload' do
…
# right before responding
Threadexample::pool.process {
# Add code that does the validation and the request
}
end
end
需要'sinatra/base'
需要“线程/池”
类Threadexample 类我对这类东西使用的一种模式是后台线程池。请注意,我使用的是Web服务器puma,它可能无法与所有其他服务器一起工作
其思想是,在响应之前的上载路由中,将代码添加到一个持久性后台线程池中,该线程池将进行验证和http请求。我从宝石上取的线池
最简单的例子:
require 'sinatra/base'
require 'thread/pool'
class Threadexample < Sinatra::Application
class << self; attr_accessor :pool end # thread pool as class variable
@pool = Thread.pool(2)
post '/upload' do
…
# right before responding
Threadexample::pool.process {
# Add code that does the validation and the request
}
end
end
需要'sinatra/base'
需要“线程/池”
类Threadexample