# Nonblocking recvfrom?

**URL:** <https://discourse.julialang.org/t/nonblocking-recvfrom/77740>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 11, 2022, 8:53am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740 "2022-03-11T08:53:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [March 11, 2022, 8:53am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/1 "2022-03-11T08:53:47Z")

</div>

Hi,  
Is it a non-blocking alternative to `recvfrom()` ?  
My application consist of two threads, where one of them is listening on a socket for incoming packets. The other thread then sends a signal to this thread after x seconds to tell it to return. This is done by using a `Channel`. But, it seems that `recvfrom` is blocking so that it don’t see that a signal is received on the channel… Does anyone have any suggestions to how I can solve this?

---

<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:** [March 11, 2022, 9:13am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/2 "2022-03-11T09:13:07Z")

</div>

`Timer` on the waiting thread? No need for inter-thread communication here.

---

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [March 11, 2022, 9:14am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/3 "2022-03-11T09:14:54Z")

</div>

No I don’t use a `Timer` here. Just two threads, where one of them need to tell the other to stop. But it will not see this stop message, because `recvfrom` is blocking…

---

<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:** [March 11, 2022, 9:32am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/4 "2022-03-11T09:32:35Z")

</div>

Yes, I got that. I was suggesting to use a `Timer` on the thread that’s waiting in `recvfrom` instead of using a `Channel` for that. Using a `Channel` means you have to actively check it, a `Timer` will interrupt you.

---

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [March 11, 2022, 9:41am UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/5 "2022-03-11T09:41:18Z")

</div>

I want my main thread be the one that tells the second thread (which has the `recvfrom`) to quit. So I don’t want my second thread to tell itself when to quit…

---

<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:** [March 11, 2022, 12:58pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/6 "2022-03-11T12:58:05Z")

</div>

Well, as far as I know, there is no nonblocking `recvfrom` (assuming you’re talking about `Sockets.recvfrom`). There’s also no good way to [interrupt a running async task](https://discourse.julialang.org/t/stop-terminate-a-sub-task-started-with-async/32193), which is why I suggested using a `Timer` to periodically interrupt the blocking behavior, then check the `Channel` for whether or not the thread should terminate or not, then relaunch a `Timer` and block again.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 11, 2022, 1:06pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/7 "2022-03-11T13:06:46Z")

</div>

recvfrom is also written in Julia, you could roll your own

[https://github.com/JuliaLang/julia/blob/master/stdlib/Sockets/src/Sockets.jl#L331-L365](https://github.com/JuliaLang/julia/blob/master/stdlib/Sockets/src/Sockets.jl#L331-L365)

---

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [March 11, 2022, 2:12pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/8 "2022-03-11T14:12:05Z")

</div>

I tried implementing a MWE now, and to my surprise this works fine. So after 2 seconds, the signal is sent, and the thread successfully quits… But, why does this work? I thought `recvfrom` was a blocking call?

```julia
using Sockets

function recv_data()
    insock = UDPSocket()
    from = Sockets.InetAddr(ip"127.0.0.1", 3000)
    bind(insock, from.host, from.port)

    while !(isready(running) && fetch(running) isa Stop)
        from, data = recvfrom(insock)
    end

    close(insock)
    return 
end

struct Stop end
running = Channel{Stop}(1)

t1 = @Threads.spawn recv_data()

sleep(2)
put!(running, Stop())

```

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 11, 2022, 2:19pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/9 "2022-03-11T14:19:58Z")

</div>

The docs say it blocks

> Read a UDP packet from the specified socket, and return the bytes received. This call blocks.

---

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [March 11, 2022, 2:22pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/10 "2022-03-11T14:22:39Z")

</div>

Yes, it does. But then I thought in MWE that `recv_data()` would not exit as it does, because in the `while`-loop the `recvfrom` will block… But it do exit successfully… Why??

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [December 16, 2022, 2:51pm UTC](https://discourse.julialang.org/t/nonblocking-recvfrom/77740/12 "2022-12-16T14:51:59Z")

</div>

Are you sure this is working? It doesn’t appear it is to me:

```julia
using Sockets

function recv_data()
       insock = UDPSocket()
       from = Sockets.InetAddr(ip"127.0.0.1", 3000)
       bind(insock, from.host, from.port)

       while !(isready(running) && fetch(running) isa Stop)
           println("listening...")
           from, data = recvfrom(insock)
       end
       println("done.")
       close(insock)
       return
   end

struct Stop end
running = Channel{Stop}(1)

t1 = @Threads.spawn recv_data()

sleep(2)
put!(running, Stop())

sleep(2)
@show istaskdone(t1)

```

Outputs:

```julia
listening...
istaskdone(t1) = false

```

Which indicates that the `recvfrom` call is blocking `t1` from exiting.
