ZMQ.jl icon indicating copy to clipboard operation
ZMQ.jl copied to clipboard

Basics Publisher Example Crashes

Open opiateblush opened this issue 1 year ago • 1 comments

The example publisher in the Basics section of the ZMQ Guide crashes if put in a function. See this:

using ZMQ

function main()
    context = Context()
    socket = Socket(context, PUB)
    bind(socket, "tcp://*:5556")
    
    while true
        zipcode = rand(10000:99999)
        temperature = rand(-80:135)
        relhumidity = rand(10:60)
        message = "$zipcode $temperature $relhumidity"
        send(socket, message)
        yield()
    end
    
    close(socket)
    close(context) 
end

if abspath(PROGRAM_FILE) == @__FILE__
    main()
end

Which results in:

ERROR: LoadError: StateError("Unknown error")
Stacktrace:
 [1] _send(socket::Socket, zmsg::Base.RefValue{ZMQ._Message}, more::Bool)
   @ ZMQ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:14
 [2] send(socket::Socket, data::String; more::Bool)
   @ ZMQ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:45
 [3] send
   @ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:42 [inlined]
 [4] main()
   @ Main C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:13
 [5] top-level scope
   @ C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:22
in expression starting at C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:21

Everthing is fine if the code is not encapsulated inside a function:

using ZMQ

context = Context()
socket = Socket(context, PUB)
bind(socket, "tcp://*:5556")

while true
    zipcode = rand(10000:99999)
    temperature = rand(-80:135)
    relhumidity = rand(10:60)
    message = "$zipcode $temperature $relhumidity"
    send(socket, message)
    yield()
end

close(socket)
close(context)

I'm on Windows 11 running Julia v1.8.5 with ZMQ v1.2.2.

Any idea what I could have done wrong? It seems to me like a rather simple change that couldn't really hurt, but apparently I have been proven wrong and could have misused the API.

opiateblush avatar Apr 07 '23 13:04 opiateblush

I can confirm the issue on my side for the publisher. I'd be pleased to have the subscriber code also.

Edit: subscribe code can be

using ZMQ

context = Context()
socket = Socket(context, SUB)

println("Collecting updates from weather server...")
connect(socket, "tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = length(ARGS) > 0 ? ARGS[1] : "10001"

subscribe(socket, zip_filter)

while true
    message = unsafe_string(recv(socket))
    println(message)
end


# Making a clean exit.
close(socket)
close(context)

femtotrader avatar Jul 12 '23 20:07 femtotrader