Recent Posts
mIRC | 27 sec ago
None | 29 sec ago
Bash | 33 sec ago
C | 35 sec ago
None | 48 sec ago
PHP | 1 min ago
ASM (NASM) | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By alexei on the 9th of Feb 2010 09:43:01 PM Download | Raw | Embed | Report
  1. if True:
  2.     from multiprocessing import Process as Worker, Queue
  3. else:
  4.     from threading import Thread as Worker
  5.     from Queue import Queue
  6.  
  7. from time import sleep
  8.  
  9. class Memoize(object):
  10.     "Cache results of a function."
  11.  
  12.     def __init__(self, f):
  13.         self.f = f
  14.         self.dict = {}
  15.  
  16.     def __call__(self, x):
  17.         if x not in self.dict:
  18.             self.dict[x] = self.f(x)
  19.  
  20.         return self.dict[x]
  21.  
  22. def func(x):
  23.     print "ENTERED: func(", x, ")"
  24.     return x**2
  25.  
  26. func = Memoize(func)
  27.  
  28. def pmap(f, xs):
  29.     """Map variant:
  30.  
  31.         >>> def f(x):
  32.         ...     sleep(1)
  33.         ...     return 10 * x
  34.  
  35.         >>> xs = range(1, 10)
  36.  
  37.         >>> pmap(f, xs)
  38.         [10, 20, 30, 40, 50, 60, 70, 80, 90]
  39.  
  40.     This would sleep 9 times as long:
  41.  
  42. #       >>> map(f, xs)
  43. #       [10, 20, 30, 40, 50, 60, 70, 80, 90]
  44.  
  45.     """
  46.  
  47.     # will return this as a result:
  48.     results = [None] * len(xs)
  49.  
  50.     def work(q, i, x):
  51.         # compute the result and post it together with identifier:
  52.         q.put((i, f(x)))
  53.  
  54.     # here the workers will post results:
  55.     q = Queue()
  56.  
  57.     # create thread/process objects, stay conservative pack everything
  58.     # explicitly into args:
  59.     workers = [ Worker(target=work, args=(q, i, x)) for i, x in enumerate(xs) ]
  60.  
  61.     # start all workers:
  62.     for w in workers:
  63.         w.start()
  64.  
  65.     # wait for completion:
  66.     for w in workers:
  67.         w.join()
  68.  
  69.         # another result must be ready, collect them:
  70.         i, fxi = q.get()
  71.         results[i] = fxi
  72.  
  73.     # now that "results" must have been set:
  74.     return results
  75.  
  76. print "Call func(2) four times:"
  77. print func(2)
  78. print func(2)
  79. print func(2)
  80. print func(2)
  81.  
  82. print "Map func() over four 2's:"
  83. print map(func, [2, 2, 2, 2])
  84. print "Map func() over four 3's:"
  85. print map(func, [3, 3, 3, 3])
  86.  
  87. print "ParMap func() over four 2's:"
  88. print pmap(func, [2, 2, 2, 2])
  89. print "ParMap func() over four 3's:"
  90. print pmap(func, [3, 3, 3, 3])
  91. print "ParMap func() over four 4's:"
  92. print pmap(func, [4, 4, 4, 4])
  93.  
  94. # python pmap.py [-v]:
  95. #   if __name__ == "__main__":
  96. #       import doctest
  97. #       doctest.testmod()
  98.  
  99. # Default options for vim:sw=4:expandtab:smarttab:autoindent:syntax
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: