Note that there are some explanatory texts on larger screens.

plurals
  1. POIdiomatic way of handling interrupts in Go using ZeroMQ
    primarykey
    data
    text
    <p>I'm learning both Go and ZeroMQ at the moment and in that spirit I'm trying to contribute Go examples for the <a href="http://zguide.zeromq.org" rel="nofollow">Zguide</a>. I'm struggling a bit with the <a href="http://zguide.zeromq.org/page%3aall#Handling-Interrupt-Signals" rel="nofollow">interrupt example</a>. I'm unsure what the idiomatic way of handling the problem would be. </p> <p>The solution I currently have is the following: I create a channel which receives the SIGINT signal. When it does I write a bool on an other channel which is used in the main loop to break. The problem is, is that Recv is blocking, the loop never gets to check the loop condition. I circumvent the problem by passing the NOBLOCK constant to Recv. But I feel there is a better way as Recv should return a EINTR when interrupted (which it doesn't as far as I can tell). You readers are much better equipped to answer this question then I am, what do you think?</p> <p>For your convenience the code I have so far:</p> <pre><code>package main import ( "os/signal" "os" "fmt" zmq "github.com/alecthomas/gozmq" ) func listenForSignals(exit_channel chan bool) { signal_channel := make(chan os.Signal) signal.Notify(signal_channel) &lt;- signal_channel fmt.Println("stopping") exit_channel &lt;- true } func main() { exit := make(chan bool) exit_signal := false go listenForSignals(exit) context, _ := zmq.NewContext() defer context.Close() socket, _ := context.NewSocket(zmq.REP) defer socket.Close() socket.Bind("tcp://*:5555") for exit_signal == false { select { case exit_signal = &lt;- exit: fmt.Println("W: interrupt received, killing server...") default: msgbytes, err := socket.Recv(zmq.NOBLOCK) fmt.Printf("%s.\n", string(msgbytes)) } } } </code></pre> <p><strong>Edit</strong> simplified the code somewhat based on feedback</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload