def player(name, table): while True: ball = yield take(table) ball.hits += 1 print name, ball.hits yield sleep(0.1) yield put(table, ball)
def test_parked_taken(self): ch = Channel() def taking(): yield sleep(0.005) yield take(ch) go(taking) self.assertEqual((yield put(ch, 42)), True)
def test_parked_closed(self): ch = Channel() def closing(): yield sleep(0.005) ch.close() go(closing) self.assertEqual((yield put(ch, 42)), False)
def test_immediate_puts_and_takes(self): ch = Channel(1) i = 0 while i < self.limit: i += 1 yield put(ch, 1) yield take(ch)
def main(): table = Channel() go(player, "ping", table) go(player, "pong", table) yield put(table, Ball()) yield sleep(1)
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 collect(i): while True: yield put(c, (yield take(i)))
def putting(): yield put(ch, 42)
def test_immediate_closed(self): ch = Channel() ch.close() self.assertEqual((yield put(ch, 42)), False)
def _do(): i = 0 while True: yield put(c, "%s %d" % (message, i)) yield sleep(random.random()) i += 1
def main(): chan = Channel() print "will block" yield put(chan, "channel") # Will not get here print (yield take(chan))
def test_immediate_buffered(self): ch = Channel(1) self.assertEqual((yield put(ch, 42)), True)
def test_putting_onto_closed_channel(self): i = 0 while i < self.limit: i += 1 yield put(self.ch, 42)
def test_immediate_taken(self): ch = Channel() def taking(): yield take(ch) go(taking) self.assertEqual((yield put(ch, 42)), True)
def f(left, right): yield put(left, 1 + (yield take(right)))
def test_immediate_buffered(self): ch = Channel(1) yield put(ch, 42) self.assertEqual((yield take(ch)), 42)
def putting(): yield sleep(0.005) yield put(ch, 42)
def main(): chan = Channel(2) yield put(chan, "buffered") yield put(chan, "channel") print (yield take(chan)) print (yield take(chan))
def collect(): while True: value, _ = yield alts([input1, input2]) yield put(c, value)
def produce(chan, value): yield sleep(0.1) yield put(chan, value)
def start(c): yield put(c, 1)