如何启动和停止 Ruby Thin 服务器?
我需要以编程方式多次启动和停止瘦服务器。我正在使用以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈!看来我已经设法通过使用 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.