Python 7.0pl5 Benutzerhandbuch

Seite von 124
Message Exchange
76
When an application is distributed across multiple machines, it is often the case that even if one ma-
chine were to be shutdown, the processes on a different machine might be able to quite happily keep
operating so long as they could still communicate. To support this, a means of setting up a distributed
version of the message exchange server is provided.
In this arrangement, each machine has its own message exchange server, with each message exchange
server connected to all others. If a machine is now shutdown or connections to one machine lost, other
machines will still be able to communicate with processes on any machines which are still accessible.
That is, loss of the message exchange server on one machine will only directly impact that machine.
To setup a distributed exchange server, the message exchange server endpoint is created as before. The
difference is that as well as listening on a port for new connections, client like connections are created
to the other message exchange servers. The aim here is to effectively create a star connected network
between the message exchange servers. That is, each message exchange server has a connection to all
other message exchange servers.
port = 11111
exchange = Exchange(netsvc.EXCHANGE_SERVER)
exchange.listen(port)
delay = 5
for host in hosts:
exchange.connect(host,port,delay)
Note that since connections are bidirectional, it is not necessary for each message exchange server to
mutually connect to each other. That is, if you have two message exchange servers, it is only necessary
for one to connect to the other. In other words, the list of remote hosts in one would be empty, where
as the list of the remote hosts in the other would be the reciprocal host. If two message exchange serv-
ers do connect to each other, this will be detected and one connection will be ignored, however it
should be avoided.
Multiple Exchange Groups
When creating a service agent, the default service audience is "
*
", indicating that knowledge of the
service should be distributed as widely as possible. One alternative is to set the service audience to the
empty string, which will always result in the service only being visible within its own process. What
occurs for other values of the service audience property depends on the exchange group assigned to a
message exchange endpoint.
By default, the exchange group of a message exchange endpoint is empty, but may be set by an option-
al argument when initialising the class. A message exchange endpoint is only able to be connected to
a complimentary message exchange endpoint which is a member of the same group. That is, a message
exchange client endpoint can only connect to a message exchange server endpoint with the same ex-
change group.