如何启动和停止 Ruby Thin 服务器?

发布于 2024-12-08 23:43:26 字数 1095 浏览 0 评论 0原文

我需要以编程方式多次启动和停止瘦服务器。我正在使用以下代码:

require "thin"

def running?
  !TCPSocket.new('127.0.0.1', 3000).close
rescue Exception
  # not running
end

loop do
    server = Thin::Server.new('0.0.0.0', 3000, lambda {|env| [200, {}, ""]})
    thread = Thread.new {server.start}
    t = Time.now
    until running?
      sleep 0.1
    end
    puts "Started in #{Time.now - t}"

    server.stop!

    t = Time.now
    while running?
      sleep 0.1
    end
    puts "Stopped in #{Time.now - t}"
end

我希望 Thin::Server#running? 会告诉我服务器何时不再运行,但我错了,必须创建自己的#running?方法。

此外,它第一次会在大约11-12秒(!?!?!)内停止,并且第二次不会打印“Started” - 例如,第二次它不会正常启动,但 Thin 打印出熟悉的线条,好像一切都会好起来的。这是我从这个脚本得到的输出:

Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
Started in 0.002001
Stopping ...
Stopped in 11.441654
Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

它只是无限期地阻塞。如何正确停止和启动服务器?

I need to start and stop Thin server programmatically more than one time. I am using the following code:

require "thin"

def running?
  !TCPSocket.new('127.0.0.1', 3000).close
rescue Exception
  # not running
end

loop do
    server = Thin::Server.new('0.0.0.0', 3000, lambda {|env| [200, {}, ""]})
    thread = Thread.new {server.start}
    t = Time.now
    until running?
      sleep 0.1
    end
    puts "Started in #{Time.now - t}"

    server.stop!

    t = Time.now
    while running?
      sleep 0.1
    end
    puts "Stopped in #{Time.now - t}"
end

I hoped that Thin::Server#running? would tell me when server is not running anymore, but i was wrong and had to create my own #running? method.

Also, it will stop in about 11-12 seconds (!?!?!) the first time and won't print "Started" for the second time - e.g. it won't start properly second time, but Thin prints the familiar lines as if everything would be ok. This is the output i'm getting from this script:

Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
Started in 0.002001
Stopping ...
Stopped in 11.441654
Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

And it's just blocking indefinitely. How can i stop and start the server properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-12-15 23:43:26

哈!看来我已经设法通过使用 EventMachine 1.0.0.beta 4.1 解决了这个问题:)

它很快,我可以使用 Thin::Server#running?而不是我的#running?方法。

Haa! It seems that i've managed to solve this problem by using EventMachine 1.0.0.beta 4.1 :)

It's fast and i can use Thin::Server#running? instead of my #running? method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文