ZIO Tutorial: Peering
Table of Contents
ZIO provides zio.Peer
which is a service to help an application "know"
what other like-minded applications exist on the network. See peer
write-up for more information. Normally application code will use a
peer via a zio.Node
(see node and node tutorial) but it may also be
used stand-alone and this tutorial will show how.
1 Create peers
Peers have a "nickname" and an optional set of headers.
p1 = zio.Peer("peer1", greeting = 'Hello world', color = 'green') p2 = zio.Peer("peer2", greeting = 'Hajimemashita', color = 'blue')
2 Get peer info
The Zyre actor runs in a background thread while the zio.Peer
interface is synchronous with the application that uses it. This
means that the peer will not "know" anything unless given a chance to
"catch up" with what Zyre has been up to.
print (p1.peers) # {} p1.drain() print (p1.peers) # {UUID('e5101ad1-e796-42f3-8d88-381cde0060f8'): # PeerInfo(uuid=UUID('e5101ad1-e796-42f3-8d88-381cde0060f8'), # nick='peer2', # headers={'greeting': 'Hajimemashita', 'color': 'blue'})}
3 Synchronizing
One use of peering is for separate asynchronous tasks to synchronize. For example, one peer may want to wait for another peer to become available before the first continues.
found = p2.waitfor('peer1') print(found) # [UUID('9a3fbd0e-54fc-4015-ae01-ebe9b15bab3a')]
4 Finding peer info
A peer may also query the network for other peers by a nickname.
found = p2.matchnick('peer2') print (found) # [UUID('e5101ad1-e796-42f3-8d88-381cde0060f8')] found = p1.matchnick('does not exist') print (found) # []
5 Stopping a peer
To make a peer disappear use its stop()
method or delete it.
p1.stop() del(p2)
Note, a peer must be explicitly stopped or destroyed by the application or else the application may hang on exit.