pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

zmq4 publisher issue, publisher is hanging while publishing continuously on socket #114

Closed jay11ca39 closed 6 years ago

jay11ca39 commented 6 years ago

Hello @pebbe

Steps to reproduce the issue:

  1. copy the below code in file: sample.go

package main


import (
    zmq "github.com/pebbe/zmq4"

    "fmt"
)

var publisher *zmq.Socket
func publishRoutine() {
     for {
         publisher.Send("Hello", 0)
         fmt.Printf("\nEvent publisher")
    }
} 

func main() {

    var err error
    publisher, err = zmq.NewSocket(zmq.PUB)
    defer publisher.Close()
    publisher.Bind("tcp://*:5562")
    if nil != err {}

    //start a go routine to continously publish the events
    go publishRoutine()

    //blocker for main exit
    for {}
}
  1. Build the code : $ go build sample.go

  2. Run the sample: $ ./sample

Expected: It should continuously publish the events. Actual: It is hanging after publishing some events.

pebbe commented 6 years ago

It's hanging on for {}. This has nothing to do with zmq. The empty for loop prevents Go to switch between Go routines.

jay11ca39 commented 6 years ago

That for{} loop is for blocking for application to exit. BTW When i tested without go routine it worked well. But is it the problem with go routine to switch between main and new thread?

jay11ca39 commented 6 years ago

Hi @pebbe , Instead of for{} loop i used channel to block exit of main program it worked well. Thanks for help.