# ZMQ.jl, Receiving message Blocked, ZMQ.recv()

**URL:** https://discourse.julialang.org/t/zmq-jl-receiving-message-blocked-zmq-recv/10672
**Category:** General Usage
**Tags:** package, zmq
**Created:** [May 3, 2018, 1:21am UTC](https://discourse.julialang.org/t/zmq-jl-receiving-message-blocked-zmq-recv/10672 "2018-05-03T01:21:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)
#### Post date: [May 3, 2018, 1:21am UTC](https://discourse.julialang.org/t/zmq-jl-receiving-message-blocked-zmq-recv/10672/1 "2018-05-03T01:21:08Z")

</div>

I try to receive message using the PUB-SUB pattern of ZMQ.jl. However, the function, ZMQ.recv(), is waiting for the message from PUB side and blocked there. Is there any way to receive the message without blocking? The code is as follows.

```julia
using ZMQ

skt_receive = Socket(ctx, SUB)
ZMQ.subscribe( skt_receive )
ZMQ.connect( skt_receive , "tcp://localhost:5556" )

msg = ZMQ.recv( skt_receive ) # blocked when there is no message sent from PUB side
@show msg

while true
     try
         msg = ZMQ.recv( skt_receive ) # blocked here
     catch
         println("no message comes.")
     end
end 

```

---

<div class="post-metadata">

### Author: ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)
#### Post date: [May 3, 2018, 2:54am UTC](https://discourse.julialang.org/t/zmq-jl-receiving-message-blocked-zmq-recv/10672/2 "2018-05-03T02:54:32Z")

</div>

I solved it by adding @async. I am not sure if it will consume a lot of memory because of creating Task object. Is there other better ways to do this?  
The Python binding supporting input parameter of ZMQ.NOBLOCK and the function recv() raises an error when there is no message coming.

```julia
using ZMQ

skt_receive = Socket(ctx, SUB)
ZMQ.subscribe( skt_receive )
ZMQ.connect( skt_receive , "tcp://localhost:5556" )

while true
     @async begin # memory consume ??
         msg = ZMQ.recv( skt_receive )    
     end
end 

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [July 12, 2018, 2:46am UTC](https://discourse.julialang.org/t/zmq-jl-receiving-message-blocked-zmq-recv/10672/3 "2018-07-12T02:46:07Z")

</div>

I would think `@async` should be very light weight.
