[zeromq-dev] Socket lifetime and Common Lisp binding

Carlos Konstanski ckonstanski at pippiandcarlos.com
Wed Mar 19 18:13:41 CET 2014


With a library like zeromq, there's no telling how much heavy lifting is
being done under the hood. The tutorial examples are all very
short-lived code snippets. In a real application that may run for weeks
or months, I wonder how best to handle the lifetime of a socket.

In the Common Lisp binding, the examples show code that looks like this
for a client REQ/REP pattern (client side):

(defun client ()
  "Connects to a socket and passes a message and waits to receive the
  \"OK\" from the server."
  (zmq:with-context (ctx 1)
    (zmq:with-socket (socket ctx zmq:req)
      (zmq:connect socket "tcp://localhost:5555")
      (zmq:send socket (make-instance 'zmq:msg
                                      :data "SELECT * FROM mytable" ))
      (let ((result (make-instance 'zmq:msg)))
        (zmq:recv socket result)
        (format t "Recieved string: '~A'~%"
                (zmq:msg-data-as-string result) )))))

In real life, would I call WITH-CONTEXT...WITH-SOCKET every time I
wanted to send a message, or would I open a socket once and keep it
around as a singleton for the lifetime of the application?

It could very well be that zeromq already does this for me, and the
WITH-SOCKET macro just fetches it. If so, this code would be very
reasonable. If not, I should probably avoid incurring the overhead of
setup/teardown for each message by keeping my own handle to an open socket.



More information about the zeromq-dev mailing list