S
Shea Martin
I want to listen for connections for 2 seconds, then timeout. Do I have
to use the Timeout module?
~S
to use the Timeout module?
~S
From: "Shea Martin said:I want to listen for connections for 2 seconds, then timeout. Do I have
to use the Timeout module?
I want to listen for connections for 2 seconds, then timeout. Do I
have to use the Timeout module?
From: "Shea Martin said:I want to listen for connections for 2 seconds, then timeout. Do
I have to use the Timeout module?
You could use select:
timeout_sec = 2.0
ios = select([@server], nil, nil, timeout_sec)
if ios
client = @server.accept
end
Note that you'll probably want to put the socket into nonblocking
mode. It's possible for select to return
"ready to read" and have accept still block, if the
client happens to disconnect in the small window between the select
and the accept.
From: "Eric Hodel said:That will be the easiest way.
Timeout.timeout 2 do
Thread.start server.accept do |s| new_connection s end
end
Bill said:Oh, hey, cool. Using the thread there looks like it
ought to avoid the issue with Timeout firing in the
middle of ensure blocks and circumventing them?
I've been avoiding Timeout like "the plague" since
running into that behavior.
Joel said:But the timeout won't stop the thread.
From: "Joel VanderWerf said:But the timeout won't stop the thread.
Oh, hey, cool. Using the thread there looks like it
ought to avoid the issue with Timeout firing in the
middle of ensure blocks and circumventing them?
I've been avoiding Timeout like "the plague" since
running into that behavior.
From: "Joel VanderWerf said:I think it might be clearer to write it as
session = Timeout.timeout 2 do
server.accept
end
Thread.start(session) do |s| new_connection s end
From: "Eric Hodel said:You've timed out, so that's what's supposed to happen
Shea said:I want to listen for connections for 2 seconds, then timeout. Do I have
to use the Timeout module?
~S
From: "Shea Martin said:<code>
Thread.new do
begin
l_exit_status = false
l_listener = TCPServer.new( HOST, p_port )
l_socket = false
Timeout.timeout( 2 ) do
l_socket = l_listener.accept
end #timeout
#use socket
l_socket.put "send you requested data"
l_exit_status = true
rescue SocketError
$log.puts "socket error"
l_exit_status = false
rescue Timeout::Error
$log.puts "you only get 2 seconds to come and get the data"
l_exit_status = false
rescue Errno::EBADF
$log.puts "could not open connection"
l_exit_status = false
ensure
l_socket.close if l_socket
end #begin/rescue
l_exit_status
end #thread/do
</code>
I don't think I have a race condition here, do I?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.