Pyro Tutorial
Pyro Tutorial
class testclass:
def mul(s, arg1, arg2): return arg1*arg2
def add(s, arg1, arg2): return arg1+arg2
def sub(s, arg1, arg2): return arg1-arg2
def div(s, arg1, arg2): return arg1/arg2
def error(s):
x=foo()
x.crash()
class foo:
def crash(s):
s.crash2('going down...')
def crash2(s, arg):
# this statement will crash on purpose:
x=arg/2
server.py
import Pyro.core
import Pyro.naming
import tst
Pyro.core.initServer()
ns=Pyro.naming.NameServerLocator().getNS()
daemon=Pyro.core.Daemon()
daemon.useNameServer(ns)
uri=daemon.connect(testclass(),"simple")
Pyro.core.initClient()
test = Pyro.core.getProxyForURI("PYRONAME://simple")
print test.mul(111,9)
print test.add(100,222)
print test.sub(222,100)
print test.div(2.0,9.0)
print test.mul('.',10)
print test.add('String1','String2')
import Numeric
class Calculator:
def setParameters(self, offset, multiplyer):
"""
__init__ can not be called remotely.
Use other method instead.
"""
self.offset = offset
self.multiplyer = multiplyer
try:
hostname = sys.argv[1]
except IndexError:
print 'Please give hostname as command line argument.'
print 'It will be used to register the calculator with the NS.'
print 'Example: hostname:node1, object_name:calculator_node1'
Pyro.core.initServer()
ns=Pyro.naming.NameServerLocator().getNS()
daemon=Pyro.core.Daemon()
daemon.useNameServer(ns)
uri=daemon.connect(CalculatorProxy(),'calculator_%s' %hostname)
class simpleSimulation:
def __init__(self, offset, multiplyer, arraySize,
deltaT, steps, nodes):
self.deltaT = deltaT
self.steps = steps
self.nodes = nodes
self.offset = offset
self.multiplyer = multiplyer
self.numbers = Numeric.array(range(arraySize), Numeric.Float)
self.results = []
self.bounds = self.findArrayBounds(self.numbers.shape[0],
len(self.nodes))
self.connectToNodes()
self.t = 0
clusterClient.py II
class simpleSimulation:
...
def findArrayBounds(self, arrayLength, numberOfThreads):
"""Split array in nearly equal sized parts.
class CalculatorThread(threading.Thread):
def __init__(self, remoteCalculator, offset, multiplyer):
threading.Thread.__init__(self)
self.remoteCalculator = remoteCalculator
self.remoteCalculator.setParameters(offset, multiplyer)
# check results
t = 0
for pyroResult in s.results:
localResult = (offset + Numeric.array(range(arraySize),
Numeric.Float) * multiplyer) * t
t += deltaT
print 'pyro results:'
print pyroResult
print 'local results:'
print localResult
Starting the calculation
Start the name server:
ns_computer$ns
*** Pyro Name Server ***
Pyro Server Initialized. Using Pyro V3.4
URI is: PYRO://192.168.1.50:9090/c0a80132044c2fc732fbadf23b086a2e
URI written to: ...Python24\Scripts\Pyro_NS_URI
Name Server started.