def test_default_value(self): ch = Channel(1) r = yield alts([ch], default=42) self.assertEqual(r.value, 42) self.assertEqual(r.channel, DEFAULT) yield put(ch, 53) r = yield alts([ch], default=42) self.assertEqual(r.value, 53) self.assertEqual(r.channel, ch)
def test_no_priority(self): nums = range(50) chs = [Channel(1) for _ in nums] for i in nums: yield put(chs[i], i) values = [] for _ in nums: values.append((yield alts(chs)).value) self.assertNotEqual(values, nums)
def main(): c = boring("Joe") while True: value, chan = yield alts([c, timeout(0.8)]) if chan is c: print value else: print "You're too slow." yield stop()
def main(): c = boring("Joe") t = timeout(5) while True: value, chan = yield alts([c, t]) if chan is c: print value else: print "You talk too much." yield stop()
def main(): for url, dt in ( ("http://www.google.com/search?q=csp", 0.01), # too little time ("http://www.google.come/search?q=csp", 10), # wrong domain name ("http://www.google.com/search?q=csp", 10), ): print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print url, dt, "seconds" c = request(url) t = timeout(dt) value, chan = yield alts([c, t]) if chan is c: result, error = value if error: print "Uhm, not good" print error else: print "Here" print result[:500] + "..." elif chan is t: print "Timeout"
def main(): chans = [] for i in range(20): chan = Channel() go(produce, chan, i) chans.append(chan) def timeout(seconds): chan = Channel() def t(): yield sleep(seconds) chan.close() go(t) return chan chans.append(timeout(0.3)) while True: value, chan = yield alts(chans) if value == CLOSED: print "time out" break else: print value
def test_selecting_on_closed_channel(self): i = 0 while i < self.limit: i += 1 yield alts([self.ch, [self.ch, 1]])
def test_identity(self): ch = identity_channel(42) r = yield alts([ch]) self.assertEqual(r.value, 42) self.assertEqual(r.channel, ch)
def collect(): while True: value, _ = yield alts([input1, input2]) yield put(c, value)