我在event-machine中使用Sinatra,并希望在收到DELETE请求后关闭服务器并退出,并返回200OK。然而,现在我还不能达到这一点,并且总是在退出之前结束u返回一个空响应。我将如何实现这一点?相关代码如下:
EM.run do
class Server < Sinatra::Base
delete '*' do
EM.defer proc{ halt 200 }, proc{ EM.stop }
end
end
Server.run!
end发生的情况是,我得到了一个空的回复,并得到了以下堆栈跟踪:
/Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `throw': uncaught throw `halt' in thread 0x7fa4225f2020 (ThreadError)
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `halt'
from instant-markdown-d.rb:39:in `DELETE *'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `call'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `spawn_threadpool'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `initialize'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `new'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `spawn_threadpool'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1023:in `defer'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:51:in `process'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from instant-markdown-d.rb:10我也尝试了许多类似的方法,但无法找到发送200,然后关闭服务器的方法。
发布于 2012-12-09 00:48:46
最终做了这样的事情:
EM.run do
class Server < Sinatra::Base
delete '*' do
EM.add_timer(0.2) do
EM.stop
exit
end
status 200
end
end
Server.run!
end这是一种黑客行为,但至少是可行的。
https://stackoverflow.com/questions/13685551
复制相似问题