How do I set the socket timeout in Ruby?

前端 未结 3 1016
日久生厌
日久生厌 2020-12-08 11:24

How do you set the timeout for blocking operations on a Ruby socket?

3条回答
  •  我在风中等你
    2020-12-08 11:32

    The solution I found which appears to work is to use Timeout::timeout:

    require 'timeout'
        ...
    begin 
        timeout(5) do
            message, client_address = some_socket.recvfrom(1024)
        end
    rescue Timeout::Error
        puts "Timed out!"
    end
    

提交回复
热议问题