sorting.py :  » Development » Pyro » Pyro-3.10 » examples » distributed-computing » tasks » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Pyro 
Pyro » Pyro 3.10 » examples » distributed computing » tasks » sorting.py
from tasks.task import PartitionableTask,TaskPartition

import time
import random
import string

#
# The 'main' task of sorting a big list of random numbers.
# It partitions the work in a way where each subtask
# sorts a chunk of the original huge list, and after that,
# all sorted chunks are merged using a single merge pass of O(n).
#
class SortTask(PartitionableTask):
      
  def __init__(self, listSize):
    PartitionableTask.__init__(self,"data sorter")
    self.size=listSize
    self.chunks=[]
    self.numchunks=0
    self.result=[]
  def split(self, numPiecesHint):
    lst=[random.choice(string.lowercase) for i in range(self.size)]
    pieces=numPiecesHint*3 # number of pieces
    chunksize=self.size/pieces
    taskparts=[]
    for i in range(pieces+1):
      chunk = lst[0:chunksize]
      del lst[0:chunksize]
      if chunk:
        taskparts.append(SortTaskPartition(chunk, i+1))
      else:
        break
    self.numchunks=len(taskparts)
    return taskparts
  def join(self,task):
    self.chunks.append(task.result)
    return False  # not done yet, also join all other tasks
  def getResult(self):
    if len(self.chunks)!=self.numchunks:
      return None  # not yet enough chunks received
    if self.result:
      return self.result
    assert sum([len(x) for x in self.chunks])==self.size, "length of combined chunks is incorrect"
    # use a single pass of the generic merge sort O(n) to get the final sorted list.
    # we sort in reverse order because removing elements from the end of a list is O(1)
    result=[]
    starttime=time.time()
    while len(result)<self.size:
      for chunk in self.chunks:
        if chunk:
          largest_chunk=chunk
          largest_elt=chunk[-1]
          break
      for chunk in self.chunks[1:]:
        if chunk and chunk[-1]>largest_elt:
          largest_elt=chunk[-1]
          largest_chunk=chunk
      result.append(largest_elt)
      del largest_chunk[-1]
    result.reverse()
    self.result=result
    print "Chunks merged in %.03f seconds." % (time.time()-starttime)
    return self.result
        


#
# The subtask responsible for sorting a certain part of the full list
#
class SortTaskPartition(TaskPartition):
  def __init__(self, unsortedList, chunknumber):
    TaskPartition.__init__(self,"sort chunk %d" % chunknumber )
    self.lst = unsortedList
    self.result=None
    self.size=len(self.lst)
  def work(self):
    prevprogress=time.time()
    result=[]
    while self.lst:
      elt=self.lst.pop()
      i=0
      while i<len(result):
        if result[i]>=elt:
          break
        i+=1
      result.insert(i, elt)
      if (time.time()-prevprogress)>0.3:
        yield len(result)
        prevprogress=time.time()
    self.result=result
      
  def progress(self, pos):
    return pos/float(self.size)



#
# The 'user interface' for the sort task.
#
class UserInterface(object):
  def begin(self):
    print "Big data array sorting."
    size=input("Enter the size of the data array (>100): ")
    return size
  def info(self, task):
    print "Sorting the data array of length %d..." % task.size
  def result(self,taskresult):
    filename="sorted.txt"
    print "\nWriting the sorted data array to",filename
    out=open(filename,"w")
    while taskresult:
      print >>out, "".join(taskresult[:70])
      del taskresult[:70]
    out.close()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.