Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import decimal
- import threading
- from Queue import PriorityQueue
- class Multiplier(threading.Thread):
- def __init__(self, a, b, prec, q):
- self.a = a
- self.b = b
- self.prec = prec
- self.q = q
- threading.Thread.__init__(self)
- def run(self):
- c = decimal.getcontext().copy()
- c.prec = self.prec
- decimal.setcontext(c)
- self.q.put((self.prec, a * b))
- return
- a = decimal.Decimal('3.14')
- b = decimal.Decimal('1.234')
- # A PriorityQueue will return values sorted by precision
- # no matter what the threads finish.
- q = PriorityQueue()
- threads = [Multiplier(a, b, i, q) for i in range(1, 6)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
- for i in range(5):
- prec, value = q.get()
- print prec, '\t', value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement