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

**URL:** <https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609>\
**Category:** General Usage\
**Created:** [November 16, 2021, 7:45pm UTC](https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609 "2021-11-16T19:45:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [November 16, 2021, 7:45pm UTC](https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609/1 "2021-11-16T19:45:03Z")

</div>

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?

Thanks,  
Paulo

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [November 17, 2021, 2:24pm UTC](https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609/2 "2021-11-17T14:24:25Z")

</div>

I found a solution to my problem:

```julia
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
                  timeout=true
                  notify(ev)
              end, delay)
        # Read a line from the socket
        @async begin
            line = readline(sock)
            push!(lst, line)
            notify(ev)
        end
        wait(ev)
    end
    
    return lst

end

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 17, 2021, 3:10pm UTC](https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609/3 "2021-11-17T15:10:57Z")

</div>

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 `wait`ing on your `Event()`.

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [November 17, 2021, 3:19pm UTC](https://discourse.julialang.org/t/reading-an-unknown-number-of-lines-from-a-socket-using-asynchronous-io/71609/4 "2021-11-17T15:19:53Z")

</div>

I will try this. Thanks
