1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import os
import socket
import sys
from math import sqrt
from Pyro5.api import Proxy, register_dict_to_class
from workitem import Workitem
# For 'workitem.Workitem' we register a deserialization hook to be able to get these back from Pyro
register_dict_to_class("workitem.Workitem", Workitem.from_dict)
WORKERNAME = "Worker_%d@%s" % (os.getpid(), socket.gethostname())
def factorize(n):
"""simple algorithm to find the prime factorials of the given number n"""
def isPrime(n):
return not any(x for x in range(2, int(sqrt(n)) + 1) if n % x == 0)
primes = []
candidates = range(2, n + 1)
candidate = 2
while not primes and candidate in candidates:
if n % candidate == 0 and isPrime(candidate):
primes = primes + [candidate] + factorize(n // candidate)
candidate += 1
return primes
def process(item):
print("factorizing %s -->" % item.data)
sys.stdout.flush()
item.result = factorize(int(item.data))
print(item.result)
item.processedBy = WORKERNAME
def main():
dispatcher = Proxy("PYRONAME:example.distributed.dispatcher")
print("This is worker %s" % WORKERNAME)
print("getting work from dispatcher.")
while True:
try:
item = dispatcher.getWork()
except ValueError:
print("no work available yet.")
else:
process(item)
dispatcher.putResult(item)
if __name__ == "__main__":
main()
|