Reading an unknown number of lines from a socket using asynchronous IO

I have an instrument (pressure scanner) that measures pressure and communicates using TCP/IP. I’m having difficulty with asynchronous IO: some operations when reading the instrument return several lines of text and I don’t know the number of lines that it will return.

Usually I would use a loop with readline and a timeout. If readling from the socket timesout, I know the instrument is not sending anything.

Any tips on this could be done?


I found a solution to my problem:

function readmanylines(sock, delay=0.4)

    lst = String[] # Store each line
    timeout = false
    while !timeout
        ev = Base.Event()

        # Set the timer to wait for `delay` seconds
        Timer(_ -> begin
              end, delay)
        # Read a line from the socket
        @async begin
            line = readline(sock)
            push!(lst, line)
    return lst


Why not just close the socket on timeout, instead of waiting & notifying an event? As such, if there is some data form the socket but nothing is read for a while, I think the readline call will still block, as at that point it’s not waiting on your Event().

I will try this. Thanks