gpt4 book ai didi

ruby - Sinatra/Thin 运行且无法使用 Ctrl-C 停止

转载 作者:数据小太阳 更新时间:2023-10-29 08:09:04 25 4
gpt4 key购买 nike

我正在创建一个在 EventMachine 中运行 Sinatra 的应用程序,当我运行准系统测试应用程序时,我无法让服务器以 Ctrl-C 结束,我必须使用 -9 或 -usr2 来终止它。

我不明白为什么 Sinatra 报告它已停止但继续为请求提供服务,或者为什么我无法使用 Ctrl-C 停止服务器。

Thin 1.6.1 与 Sinatra 1.4.4 停止消息但继续

== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Stopping ...
== Sinatra has ended his set (crowd applauds)
Ping!
^CPing!
Stopping ...
Ping!
^CStopping ...

这是我用来生成输出的准系统测试应用程序

# Run with 'ruby test.rb'
require 'eventmachine'
require 'sinatra/base'
require 'thin'

class NeverStops < Sinatra::Base
settings.logging = true

configure do
set :threaded, true
end

get '/foobar' do
'Foobar'
end
end

EM.run do

# Does nothing
#trap(:INT) { EM::stop(); exit }
#trap(:TERM) { EM::stop(); exit }
#trap(:KILL) { EM::stop(); exit }

EventMachine.add_periodic_timer 2 do
puts 'Ping!'
end

NeverStops.run!
end

降级 Thin 或 Sinatra 有不同的结果

带有 Sinatra 1.4.3 的 Thin 1.6.1 没有停止的消息,但仍然不会死(确实是死亡证明)

== Sinatra/1.4.3 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Ping!
^CPing!
Stopping ...
Ping!

Thin 1.5.1 和 Sinatra 1.4.4 只是停止

== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
>> Stopping ...
== Sinatra has ended his set (crowd applauds)

Thin 1.5.1 与 Sinatra 1.4.3 WORKS

== Sinatra/1.4.3 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
Ping!
Ping!
Ping!
^C>> Stopping ...

== Sinatra has ended his set (crowd applauds)

我已将我的 gems 更新到最新版本,并尝试降级各种 gems,例如 EventMachine 和 Rack,以查看我得到的结果,并没有什么不同。

版本

  • OSX 10.8.5 和 Ubuntu 12.04.1
  • ruby 2.0.0p247 和 1.9.3p194
  • 事件机器 1.0.3
  • 机架 1.5.2
  • 西纳特拉 1.4.4
  • 瘦身 1.6.1
  • 倾斜 1.4.1

最佳答案

此问题特定于较新版本的 Thin(请注意 v1.5.1 不会出现此行为)。此行为是在 1.6 中引入的,并且记录了类似的问题 here .有问题的代码遵循与上游 issue 中提到的相同模式.

问题的 TL;DR 版本: Thin 会停止服务器,但不会停止 react 器循环(因为它不“拥有” react 器)。允许 Thin 拥有其 react 器循环是可能的,在这种情况下,您将恢复所需的行为(如 1.5.1 中所示)。为此,您必须在不包含 EM#run { } 的情况下启动 Thin,这将允许 Thin 启动(并随后拆除) react 器循环。

在这种情况下,可以将周期性“ping”想象成与 Thin 共享 react 器循环的单独应用程序。他们都不能声称拥有反应堆回路。 Thin 在没有启动 react 器的情况下停止所有其他应用程序并退出 react 器是错误的。然后由用户负责处理信号并根据需要终止各个应用程序并最终停止 react 器循环(导致进程退出)。

希望这个解释对您有所帮助!

关于ruby - Sinatra/Thin 运行且无法使用 Ctrl-C 停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com