# Reading sockets asynchronously

**URL:** https://discourse.julialang.org/t/reading-sockets-asynchronously/92096
**Category:** General Usage
**Tags:** networking, multithreading, sockets, tcp
**Created:** [December 24, 2022, 11:04pm UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096 "2022-12-24T23:04:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [December 24, 2022, 11:04pm UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/1 "2022-12-24T23:04:18Z")

</div>

I’m using `readavailable()` within a loop to read messages from a tcp socket as they come in.  
Is there a version that doesn’t require a loop ? i.e. something that will call a function asynchronously with the incoming message as and when its received ?

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 2, 2023, 3:21am UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/2 "2023-01-02T03:21:19Z")

</div>

readavailable is pretty much the only API that does not do that (reading asynchronously). Almost any other read function would do just that, resuming the Task at exactly the point it paused and calling the next function

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [January 10, 2023, 8:52am UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/3 "2023-01-10T08:52:25Z")

</div>

Can you give an example?

Suppose I have function f(message),  
and 10 mesaages arrive on the socket almost simultaneously.

Can you give and example of a function that would call f() for each message simultaneously.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 10, 2023, 1:32pm UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/4 "2023-01-10T13:32:11Z")

</div>

No, CPU cores can’t do things (like receive messages or call functions) simultaneously, due to physics

---

<div class="post-metadata">

### Author: ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)
#### Post date: [January 10, 2023, 3:14pm UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/5 "2023-01-10T15:14:53Z")

</div>

See the manual [here](https://docs.julialang.org/en/v1/manual/networking-and-streams/#Asynchronous-I/O)? In that example replace `println` with your `f(message)` .

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 10, 2023, 5:00pm UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/6 "2023-01-10T17:00:22Z")

</div>

What Jameson is getting at is that you don’t (in any language) actually write a program that reads messages simultaneously, what you do is read a message one at a time and defer the processing of that message to be done either now or later (concurrent but not simultaneous), whenever the CPU has idle cycles. So what you would do is something like this:

```julia
@sync while !eof(sock)
    msg = read_message(sock)
    @async process_message(msg)
end

```

Here `read_message` is a function that reads a single message from the socket and `process_message` is a function that takes a message and does something in response. The `@async` annotation means that the processing of one message can be done concurrently with the processing of other messages. This means that the processing continues until it reaches a blocking operation and then the scheduler looks at what has become unblocked and continues with that. If you want to (potentially) use threads rather than just non-blocking I/O, then you can replace `@async` with `Threads.@spawn`.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [January 12, 2023, 5:05am UTC](https://discourse.julialang.org/t/reading-sockets-asynchronously/92096/7 "2023-01-12T05:05:24Z")

</div>

Thanks so much Stefan. Threads.@spawn works really well 🙂

related question…

Does the Threads.@threads loop macro have an equivalent for broadcasting ?  
Would it be possible to speed up something like `exp.( 1:1E7 )` by making it multi-threaded ?

I found broadcast is already faster than the simple multi-threaded loop below

```julia
n = 10_000_000 
Xin = 1:n
Xout = zeros(n)

Threads.@threads for i = 1:n
    Xout[i] = exp( Xin[i] )
end

```
