+# Generator implementation using threads
+Killed = 'Generator.Killed'
+ def __init__(self, func, args):
+ self.getlock = thread.allocate_lock()
+ self.putlock = thread.allocate_lock()
+ thread.start_new_thread(self._start, ())
+ apply(self.func, (self,) + self.args)
+ # Called by producer for each value; raise Killed if no more needed
+ raise TypeError, 'put() called on killed generator'
+ self.getlock.release() # Resume consumer thread
+ self.putlock.acquire() # Wait for next get() call
+ # Called by producer to get next value; raise EOFError if no more
+ raise TypeError, 'get() called on killed generator'
+ self.putlock.release() # Resume producer thread
+ self.getlock.acquire() # Wait for value to appear
+ raise EOFError # Say there are no more values
+ # Called by consumer if no more values wanted
+ raise TypeError, 'kill() called on killed generator'
+ return Generator(self.func, self.args)
+ k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
+ p, q, k = k*k, 2L*k+1L, k+1L
+ a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
+ a, a1 = 10L*(a%b), 10L*(a1%b1)
+ for i in range(10): print g.get(),