I got a question regarding threads and the function gets. Lets say I got
two threads running simultaneously. Both are reading from the same
TCPSocket. Will the threads be able to read from the same TCPSocket at
the same time, or will the first thread lock it from the other.
If so, how can I check if it has been locked?
Example code
def user_idle_time(connection, name, conn)
Timeout::timeout(3.5) do
connection.send("......."} \r\n", 0)
while server_response = connection.gets
......
end
end
rescue Timeout::Error
return "Unknown"
end
I got a question regarding threads and the function gets. Lets say I got
two threads running simultaneously. Both are reading from the same
TCPSocket. Will the threads be able to read from the same TCPSocket at
the same time, or will the first thread lock it from the other.
Don’t do this. If there’s a lot of data coming in, probably one thread
will get some and another thread get the rest.
Example code
def user_idle_time(connection, name, conn)
> Timeout::timeout(3.5) do
> connection.send("......."} \r\n", 0)
> while server_response = connection.gets
> ......
> end
> end
> rescue Timeout::Error
> return "Unknown"
> end
There’s only one thread reading from the socket there. Why do you need
another thread reading from it at the same time?
If the protocol is asynchronous (i.e. the far end can send messages to
you on its own initiative, as well as responses to requests you send)
then you probably want a demultiplexor thread. Then this can talk to
other threads by means of Queues.
Regards,
Brian.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.