On Tue, Aug 21, 2012 at 3:17 PM, tammy roberts2 <[email protected]> wrote:
>> Aha.  The issue with that is that you can read only with one thread at
>> a time from the socket.  Since you created a thread after socket
>> accept and that thread loops reading this should be the only thread
>> fetching data from the socket.  Sending is a different story (but btw.
>> you should also properly synchronize to make sure only one thread
>> writes at a time).
>
> Please correct me if I am wrong, but shouldn't there be no problem as
> the yield is inside the thread itself? It seems like the listen method
> can not pass the socket to the caller until it is done reading from it.

Right you are.  But this is a quite inefficient approach IMHO: you
must read all announced messages before you can process them.  Also,
what happens if there are messages after the initially announced n
messages?  Processing will usually be easier and more efficient if you
yield each message as soon as it has arrived.  But then again, I do
know nothing about your use case and your requirements.

>> No.  Method #listen does not yield.  It's the reader thread which you
>> start in #listen.  And note that this can be a problem if the block
>> you pass in is not prepared to be executed concurrently.
>
> Ah good observation. That makes it seem like the first issue you point
> out will not come into play though, because the reader thread in listen
> is what yields the socket so it seems it will not yield it until it is
> done reading from it. Am I maybe misunderstanding something here?

No, apparently not.  But see above.

> I am going to switch my code to read and write as per your suggestion. I
> notice that these methods do not seem to be documented in the Ruby
> socket documentation, could you maybe point me towards some
> documentation regarding them please? I will post my networking code once
> more when it is finished up, again thanks for the tips and advice.

Which methods are not documented?  If you refer to #read and #write,
please observe this:

$ irb19 -r socket
Ruby version 1.9.3
irb(main):001:0> TCPSocket.instance_method(:read)
=> #<UnboundMethod: TCPSocket(IO)#read>
irb(main):002:0> TCPSocket.instance_method(:write)
=> #<UnboundMethod: TCPSocket(IO)#write>

In other words: these methods are inherited from class IO.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to