Capturing Ctrl-c in ruby

前端 未结 5 1638
慢半拍i
慢半拍i 2020-12-02 06:02

I was passed a long running legacy ruby program, which has numerous occurrences of

begin
  #dosomething
rescue Exception => e
  #halt the exception\'s pr         


        
5条回答
  •  星月不相逢
    2020-12-02 06:33

    Handling Ctrl-C cleanly in Ruby the ZeroMQ way:

    #!/usr/bin/env ruby
    
    # Shows how to handle Ctrl-C
    require 'ffi-rzmq'
    
    context = ZMQ::Context.new(1)
    socket = context.socket(ZMQ::REP)
    socket.bind("tcp://*:5558")
    
    trap("INT") { puts "Shutting down."; socket.close; context.terminate; exit}
    
    puts "Starting up"
    
    while true do
      message = socket.recv_string
      puts "Message: #{message.inspect}"
      socket.send_string("Message received")
    end
    

    Source

提交回复
热议问题