Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basics Publisher Example Crashes #223

Closed
opiateblush opened this issue Apr 7, 2023 · 1 comment · Fixed by #229
Closed

Basics Publisher Example Crashes #223

opiateblush opened this issue Apr 7, 2023 · 1 comment · Fixed by #229

Comments

@opiateblush
Copy link

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.

@femtotrader
Copy link

femtotrader commented Jul 12, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants