Jameson, thanks for your reply. Basically, the question is what exactly to 
do in a task that handles a socket. E.g. I can't do just:

@async begin
    socket = connect(...)
    while true
        data = read(socket, Int64)
        process_data(data)
    end
end
this only works if there's something to read all the time. But if nothing 
is written to the socket for a while, `read()` will fail with timeout.

This is why I'm looking for a way to make a call to `read()` only when 
there's actually something to read. So far I came to the following function 
that does exactly this, but may have (configurable) delay:

"""
Parameters:
 * sockets - list of sockets to select from
 * readfn - function that takes socket and returns read result
 * sleep_time - (optional) - interval between socket checks
 * channel_size - (optional) size of a result channel, effectively, buffer 
size
Returns:
 * channel with objects that were read from sockets
"""
function select(sockets::Vector{TCPSocket}, readfn::Function;
                sleep_time::Float64=0.1, channel_size::Int64=1024)
    result_channel = Channel(channel_size)
    for s in sockets
        @async begin
            Base.start_reading(s)
            while isopen(s)
                while nb_available(s) == 0 sleep(sleep_time) end
                result = readfn(s)
                put!(result_channel, result)
            end
        end
    end
    return result_channel
end

Better implementation that would remove possible delay is welcome.


On Tuesday, May 17, 2016 at 8:18:16 AM UTC+3, Jameson wrote:
>
> Launch each socket into it's own task (@async) and let Julia's 
> green-threading manage the poll/select for you internally.
>
>
> On Friday, May 13, 2016 at 8:08:01 PM UTC-4, Andrei Zh wrote:
>>
>> 1. I found out that TCPSocket contains several conditions and callbacks, 
>> but I couldn't find any docs or example of their usage. 
>> 2. I also found #6563 <https://github.com/JuliaLang/julia/issues/6563> that 
>> discusses exactly the problem of listening to multiple conditions / 
>> channels. 
>>
>> So if I manage to create a callback for incoming data, I will be able to 
>> borrow some ideas from (1) and come up with a `select` function that 
>> satisfies my needs. Does anybody have an example of using TCPSocket's 
>> callbacks/conditions?
>>
>> On Thursday, May 12, 2016 at 12:45:44 AM UTC+3, Andrei Zh wrote:
>>>
>>> Given several TCPSocket-s, how can I sequentially poll them and read the 
>>> first that becomes readable?
>>>
>>> I believe *nix's poll/select functions provide something similar for 
>>> files (and Julia has `poll_fd`), but what is the best way to approach this 
>>> task for sockets?
>>>
>>

Reply via email to