~ubuntu-branches/debian/sid/pyro/sid

« back to all changes in this revision

Viewing changes to examples/distributed-computing/worker.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet, Carl Chenet, Jakub Wilk
  • Date: 2010-09-14 01:04:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100914010428-02r7p1rzr7jvw94z
Tags: 1:3.9.1-2
[Carl Chenet]
* revert to 3.9.1-1 package because of the development status 
  of the 4.1 package is unsuitable for stable use
  DPMT svn #8557 revision (Closes: #589172) 
* added debian/source
* added debian/source/format
* package is now 3.0 (quilt) source format
* debian/control
  - Bump Standards-Version to 3.9.1

[Jakub Wilk]
* Add ‘XS-Python-Version: >= 2.5’ to prevent bytecompilation with python2.4
  (closes: #589053).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os,socket,sys
2
 
from math import sqrt
3
 
import Queue
4
 
import Pyro
5
 
from workitem import Workitem
6
 
 
7
 
WORKERNAME = "Worker_%d@%s" % (os.getpid(), socket.gethostname())
8
 
 
9
 
def factorize(n):
10
 
    """simple algorithm to find the prime factorials of the given number n"""
11
 
    def isPrime(n):
12
 
        return not [x for x in xrange(2,int(sqrt(n))+1) if n%x == 0]
13
 
    primes = []
14
 
    candidates = xrange(2,n+1)
15
 
    candidate = 2
16
 
    while not primes and candidate in candidates:
17
 
        if n%candidate == 0 and isPrime(candidate):
18
 
            primes = primes + [candidate] + factorize(n/candidate)
19
 
        candidate += 1            
20
 
    return primes
21
 
    
22
 
def process(item):
23
 
    print "factorizing",item.data,"-->",
24
 
    sys.stdout.flush()
25
 
    item.result=factorize(int(item.data))
26
 
    print item.result
27
 
    item.processedBy = WORKERNAME
28
 
 
29
 
def main():
30
 
    dispatcher = Pyro.core.Proxy("PYRONAME:example.distributed.dispatcher")
31
 
    print "This is worker",WORKERNAME
32
 
    print "getting work from dispatcher."
33
 
    while True:
34
 
        try:
35
 
            item = dispatcher.getWork()
36
 
        except Queue.Empty:
37
 
            print "no work available yet."
38
 
        else:
39
 
            process(item)
40
 
            dispatcher.putResult(item)
41
 
            
42
 
if __name__=="__main__":
43
 
    main()