Version 10 of ZeroMQ

Updated 2012-04-27 08:18:12 by edit

ØMQ, also written as ZeroMQ or 0MQ, is an open source (LGPL) [L1 ] socket/messaging framework.

Bindings for version 2.1, 2.2 and 3.1 can be found at http://github.com/jdc8/tclzmq

There is also a Tcl binding [L2 ] for V1.0.

A publishing example, also found in the ØMQ Guide :

#
# Weather update server
# Binds PUB socket to tcp:#*:5556
# Publishes random weather updates
#

package require zmq

# Prepare our context and publisher
zmq context context
zmq socket publisher context PUB
publisher bind "tcp://*:5556"
publisher bind "ipc://weather.ipc"

# Initialize random number generator
expr {srand([clock seconds])}

while {1} {
    # Get values that will fool the boss
    set zipcode [expr {int(rand()*100000)}]
    set temperature [expr {int(rand()*215)-80}]
    set relhumidity [expr {int(rand()*50)+50}]
    # Send message to all subscribers
    set data [format "%05d %d %d" $zipcode $temperature $relhumidity]
    if {$zipcode eq "10001"} {
        puts $data
    }
    zmq message msg -data $data
    publisher send_msg msg
    msg close
}

publisher close
context term

with corresponding client:

#
# Weather update client
# Connects SUB socket to tcp:#localhost:5556
# Collects weather updates and finds avg temp in zipcode
#

package require zmq

# Socket to talk to server
zmq context context
zmq socket subscriber context SUB
subscriber connect "tcp://localhost:5556"

# Subscribe to zipcode, default is NYC, 10001
if {[llength $argv]} {
    set filter [lindex $argv 0]
} else {
    set filter "10001"
}

subscriber setsockopt SUBSCRIBE $filter

# Process 100 updates
set total_temp 0
for {set update_nbr 0} {$update_nbr < 100} {incr update_nbr} {
    zmq message msg
    subscriber recv_msg msg
    lassign [msg data] zipcode temperature relhumidity
    puts [msg data]
    msg close
    incr total_temp $temperature
}

puts "Averate temperatur for zipcode $filter was [expr {$total_temp/$update_nbr}]F"

subscriber close
context term

Rewriting the client to process the messages from the publisher asynchronously:

#
# Weather update client
# Connects SUB socket to tcp:#localhost:5556
# Collects weather updates and finds avg temp in zipcode
#

package require zmq

# Socket to talk to server
zmq context context
zmq socket subscriber context SUB
subscriber connect "tcp://localhost:5556"

# Subscribe to zipcode, default is NYC, 10001
if {[llength $argv]} {
    set filter [lindex $argv 0]
} else {
    set filter "10001"
}

proc get_weather {} {
    global total_temp cnt done
    set data [subscriber recv]
    puts $data
    lassign $data zipcode temperature relhumidity
    incr total_temp $temperature
    incr cnt
    if {$cnt >= 10} {
        set done 1
    }
}

subscriber setsockopt SUBSCRIBE $filter
set total_temp 0
set cnt 0
subscriber readable get_weather

# Process 100 updates
vwait done

puts "Averate temperatur for zipcode $filter was [expr {$total_temp/$cnt}]F"

subscriber close
context term

AK - 2012-04-04 16:24:27

Does this mean that GSoC Idea: Updated Tcl bindings for ZeroMQ is out of date ?


jdc - 2012-04-04 18:50:34

I guess so. All ZeroMQ commands are wrapped and most of the examples are ported to Tcl. Still work to do to make the package thread safe and to improve the asynchronous processing of messages.