def test_buf_overfull(self): c = Chan(5) quickthread(sayset, c, list(range(20)), delay=0) time.sleep(0.1) # Fill up buffer results = list(c) self.assertEqual(results, list(range(20)))
def timer(duration): def timer_thread(chan, duration): time.sleep(duration) chan.put(time.time()) c = Chan() quickthread(timer_thread, c, duration) return c
def fan_in(*input_list): def forward(input, output): while True: output.put(input.get()) c = Chan() for input in input_list: quickthread(forward, input, c) return c
def fan_in(input1, input2): def forwarder(input, output): while True: output.put(input.get()) c = Chan() quickthread(forwarder, input1, c) quickthread(forwarder, input2, c) return c
def fan_in(input1, input2): c = Chan() def forward(): while True: chan, value = chanselect([input1, input2], []) c.put(value) quickthread(forward) return c
def test_simple(self): chan = Chan() results = [] quickthread(accumulator, chan, results) chan.put("Hello") time.sleep(0.01) # Technically unsafe self.assertEqual(len(results), 1) self.assertEqual(results[0], "Hello")
def test_select_and_closed(self): a, b, c = [Chan() for _ in range(3)] out = Chan() quickthread(sayset, a, [0, 1, 2], delay=0.01, __name='sayset1') quickthread(sayset, b, [3, 4, 5], delay=0.01, __name='sayset2') quickthread(sayset, c, [6, 7, 8], delay=0.01, __name='sayset2') def fanin_until_closed(inchans, outchan): inchans = inchans[:] while inchans: try: _, val = chanselect(inchans, []) out.put(val) except ChanClosed as ex: inchans.remove(ex.which) out.close() quickthread(fanin_until_closed, [a, b, c], out, __name='fanin') into = [] acc = quickthread(accumulator, out, into) acc.join(10) self.assertFalse(acc.is_alive()) results = set(into) self.assertEqual(len(results), 9) self.assertEqual(results, set(range(9)))
def boring(message): def sender(message, c): i = 0 while True: c.put("%s: %d" % (message, i)) time.sleep(0.2 * random.random()) i += 1 c = Chan() quickthread(sender, message, c) return c
def boring(msg): c = Chan() def sender(): i = 0 while True: c.put("%s: %d" % (msg, i)) time.sleep(1.5 * random.random()) i += 1 quickthread(sender) return c
def boring(msg): c = Chan() wait_for_it = Chan() def sender(): i = 0 while True: c.put(Message("%s: %d" % (msg, i), wait_for_it)) time.sleep(0.2 * random.random()) wait_for_it.get() i += 1 quickthread(sender) return c
def test_iter_and_closed(self): c = Chan() quickthread(sayset, c, [1, 2, 3], delay=0) def listener(): it = iter(c) self.assertEqual(next(it), 1) self.assertEqual(next(it), 2) self.assertEqual(next(it), 3) self.assertRaises(StopIteration, it.__next__) t = quickthread(listener) time.sleep(0.1) self.assertFalse(t.is_alive())
def boring(msg, quit): c = Chan() def sender(): i = 0 while True: time.sleep(1.0 * random.random()) chan, _ = chanselect([quit], [(c, "%s: %d" % (msg, i))]) if chan == quit: quit.put("See you!") i += 1 quickthread(sender) return c
def test_nothing_lost(self): phrases = ['Hello_%03d' % x for x in range(1000)] firstchan = Chan() chan_layer1 = [Chan() for i in range(6)] lastchan = Chan() sayer = quickthread(sayset, firstchan, phrases, delay=0.001, __name='sayer') # Distribute firstchan -> chan_layer1 for i in range(12): outchans = [chan_layer1[(i+j) % len(chan_layer1)] for j in range(3)] quickthread(distributer, [firstchan], outchans, delay_max=0.005, __name='dist_layer1_%02d' % i) # Distribute chan_layer1 -> lastchan for i in range(12): inchans = [chan_layer1[(i+j) % len(chan_layer1)] for j in range(0, 9, 3)] quickthread(distributer, inchans, [lastchan], delay_max=0.005, __name='dist_layer2_%02d' % i) results = [] quickthread(accumulator, lastchan, results, __name='accumulator') sayer.join(10) self.assertFalse(sayer.is_alive()) time.sleep(1) # Unsafe. Lets the data propagate to the accumulator # Checks that none are missing, and there are no duplicates. self.assertEqual(len(results), len(phrases)) self.assertEqual(set(results), set(phrases))
def example_daisy(): def f(left, right): left.put(1 + right.get()) N = 1000 # Python's threads aren't that lightweight leftmost = Chan() rightmost = leftmost left = leftmost for i in xrange(N): right = Chan() quickthread(f, left, right) left = right def putter(): right.put(1) quickthread(putter) print leftmost.get()
def main(): FetcherCls = MockFetcher merged = Merged([ Subscription(FetcherCls('blog.golang.org')), Subscription(FetcherCls('googleblog.blogspot.com')), Subscription(FetcherCls('googledevelopers.blogspot.com'))]) # Close after a while def close_later(): time.sleep(3) print("Closed: {}".format(merged.close())) quickthread(close_later) for it in merged.updates(): print("{} -- {}".format(it.channel, it.title)) time.sleep(0.1) print("Still active: (should only be _MainThread and timeouts)") for active in threading._active.itervalues(): print(" {}".format(active))
def main(): FetcherCls = MockFetcher merged = Merged([ Subscription(FetcherCls('blog.golang.org')), Subscription(FetcherCls('googleblog.blogspot.com')), Subscription(FetcherCls('googledevelopers.blogspot.com')) ]) # Close after a while def close_later(): time.sleep(3) print("Closed: {}".format(merged.close())) quickthread(close_later) for it in merged.updates(): print("{} -- {}".format(it.channel, it.title)) time.sleep(0.1) print("Still active: (should only be _MainThread and timeouts)") for active in threading._active.itervalues(): print(" {}".format(active))
def test_nothing_lost(self): phrases = ['Hello_%03d' % x for x in range(1000)] firstchan = Chan() chan_layer1 = [Chan() for i in range(6)] lastchan = Chan() sayer = quickthread(sayset, firstchan, phrases, delay=0.001, __name='sayer') # Distribute firstchan -> chan_layer1 for i in range(12): outchans = [ chan_layer1[(i + j) % len(chan_layer1)] for j in range(3) ] quickthread(distributer, [firstchan], outchans, delay_max=0.005, __name='dist_layer1_%02d' % i) # Distribute chan_layer1 -> lastchan for i in range(12): inchans = [ chan_layer1[(i + j) % len(chan_layer1)] for j in range(0, 9, 3) ] quickthread(distributer, inchans, [lastchan], delay_max=0.005, __name='dist_layer2_%02d' % i) results = [] quickthread(accumulator, lastchan, results, __name='accumulator') sayer.join(10) self.assertFalse(sayer.is_alive()) time.sleep(1) # Unsafe. Lets the data propagate to the accumulator # Checks that none are missing, and there are no duplicates. self.assertEqual(len(results), len(phrases)) self.assertEqual(set(results), set(phrases))
def test_buf_kept_empty(self): c = Chan(5) quickthread(sayset, c, list(range(20)), delay=0.02) results = list(c) self.assertEqual(results, list(range(20)))