Advertise & Discover Services: Advanced Usage¶
The examples here all refer to the networkzero.discovery
module.
Discover a group of services¶
On machine A¶
import networkzero as nw0
alice = nw0.advertise("chat/alice")
On machine B¶
import networkzero as nw0
bob = nw0.advertise("chat/bob")
On machine C¶
import networkzero as nw0
services = dict(nw0.discover_group("chat"))
print(services)
Discussion¶
By adopting a common convention, it is possible to group advertised services together. This could be used, as in this example, to identify all members of a particular chat session. Or—in a classroom or club situation—to have teams identify their own services so as not to confuse them with other team’s services of the same name.
Use a different separator for a group of services¶
import networkzero as nw0
alice = nw0.advertise("chat:alice")
bob = nw0.advertise("chat:bob")
doug = nw0.advertise("chat:doug")
services = dict(nw0.discover_group("chat", separator=":"))
print(services)
Discussion¶
Perhaps it’s not convenient to use the “/” character to separate the group from the service name. You can use any character to separate the parts of the name. Then you specify that character when you call discover_group.
Dynamically discover services¶
Node A¶
import networkzero as nw0
name = "A"
nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")
while True:
command = nw0.wait_for_message_from(master, autoreply=True)
#
# ... something goes wrong
#
raise RuntimeError
Node B¶
import networkzero as nw0
name = "B"
nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")
while True:
command = nw0.wait_for_message_from(master, autoreply=True)
#
# Do useful things until told to stop
#
if command == "STOP":
break
Node C¶
import networkzero as nw0
name = "C"
#
# This machine only starts up a few seconds after the others
#
time.sleep(3)
nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")
while True:
command = nw0.wait_for_message_from(master, autoreply=True)
#
# Do useful things until told to stop
#
if command == "STOP":
break
Master¶
import networkzero as nw0
name = "cluster/master"
address = nw0.advertise(name)
#
# On the cluster master
#
nodes = set(nw0.discover_group("cluster", exclude=name))
old_nodes = nodes
print("Nodes:", ", ".join(nodes))
#
# Send a command to A .... which will unaccountably fail
#
node_a = nw0.discover("cluster/A")
nw0.send_message_to(node_a, "STOP")
#
# Wait a few seconds for node C to wake up
#
time.sleep(5)
#
# On the cluster master
#
nodes = set(nw0.discover_group("cluster"))
for name, address in old_nodes - nodes:
print("%s has left the cluster" % name)
for name, address in nodes - old_nodes:
print("%s has joined the cluster" % name)
for name, address in nodes:
nw0.send_message_to(address, "STOP")
Discussion¶
A cluster is a group of computers working together. Each computer registers a service called “cluster/X” where “X” is the computer’s name. If that machine stops running, its advert will time out and no longer be available.
The computer which is co-ordinating the cluster checks every so often to see which computers have left the cluster and which have joined.