def main(): for i in range(n): go(test, i) for i in range(n): for i in range(n): running.recv() if i == n -1: m.lock() exit = True m.unlock() ret = select(awake.if_recv(), default) assert ret != awake.if_recv(), "coroutine not asleep" m.lock() c.broadcast() m.unlock() seen = {} for i in range(n): g = awake.recv() assert g not in seen, "coroutine woke up twice" seen[g] = True ret = select(running.if_recv(), default) assert ret != running.if_recv(), "coroutine did not exist" c.broadcast()
def main(): m = Mutex() c = makechan() for i in range(10): go(hammer_mutex, m, 1000, c) for i in range(10): c.recv()
def main(): c = makechan(5, label="c") go(test, c) for i in range(5): c.send(i) c.send("QUIT")
def main(): ch = makechan() for each in numbers: go(counter, each, ch) for each in numbers: ch.send(None)
def main(): a = [7, 2, 8, -9, 4, 0] c = makechan() go(sum, a[:int(len(a)/2)], c) go(sum, a[int(len(a)/2):], c) x, y = c.recv(), c.recv() print(x, y, x+y)
def main(): a = [7, 2, 8, -9, 4, 0] c = makechan() go(sum, a[:int(len(a) / 2)], c) go(sum, a[int(len(a) / 2):], c) x, y = c.recv(), c.recv() print(x, y, x + y)
def main(): c = makechan(10) go(sender, c) unblocked_recv = [] for i in range(11): unblocked_recv.append(c.recv()) assert len(unblocked_recv) == 11
def main(): X, Y = makechan(), makechan() go(nest, X, Y) X.send(13) dprint('tnp ==== 2') res = Y.recv() dprint('tnp ==== 3') assert res == 13 if SHOW_STRANGE: raise Exception('force prints')
def main(): c = makechan() quit = makechan() def f(): for i in range(10): print(c.recv()) quit.send(0) go(f) fibonacci(c, quit)
def main(): c = makechan() once = Once() o = AtomicLong(0) for i in range(10): go(test, once, o, c) for i in range(10): c.recv() assert o == 1
def main(): c = makechan() quit = makechan() def f(): for i in range(10): rlist.append(c.recv()) print(rlist) quit.send(0) go(f) fibonacci(c, quit)
def main(): c = makechan(5, label="c") quit = makechan(label="quit") def f(): for i in range(5): v = c.recv() rlist.append(v) quit.send(0) go(f) test(c, quit)
def wait_two(X, Y, Ret_chan): Barrier = makechan() go(sleep, X, Barrier) go(sleep, Y, Barrier) dprint('twt_W ==== 1') ret = Barrier.recv() dprint('twt_W ==== 2') if ret[0] == X: Ret_chan.send((1, ret[1])) else: Ret_chan.send((2, ret[1])) dprint('twt_W ==== 3')
def main(): go(test) for i in range(n): rlocked.recv() rl.unlock() ret = select(wlocked.if_recv(), default) assert ret != wlocked.if_recv(), "RLocker didn't read-lock it" rl.unlock() wlocked.recv() ret = select(rlocked.if_recv(), default) assert ret != rlocked.if_recv(), "RLocker didn't respect the write lock" wl.unlock()
def nest(X, Y): X2, Y2 = makechan(), makechan() go(pipe, X2, Y2) dprint('tnp_N ==== 1') X_Val = X.recv() dprint('tnp_N ==== 2') X2.send(X_Val) dprint('tnp_N ==== 3') Y2_Val = Y2.recv() dprint('tnp_N ==== 4') Y.send(Y2_Val) dprint('tnp_N ==== 5')
def test_schedule(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) gosched() assert output == [(1,), (2,)]
def test_run(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) run() assert output == [(1,), (2,)]
def test_run(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) run() assert output == [(1, ), (2, )]
def test_schedule(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) gosched() assert output == [(1, ), (2, )]
def main(): X, Y = makechan(), makechan() Ret_chan = makechan() go(wait_two, X, Y, Ret_chan) dprint('twt ==== 1') Y.send(42) dprint('twt ==== 2') X.send(42) dprint('twt ==== 3') value = Ret_chan.recv() dprint('twt ==== 4') assert value == (2, 42)
def test_run_class(): output = [] def print_(*args): output.append(args) class Test(object): def __call__(self, i): print_(i) t = Test() go(t, 1) go(t, 2) run() assert output == [(1, ), (2, )]
def test_run_class(): output = [] def print_(*args): output.append(args) class Test(object): def __call__(self, i): print_(i) t = Test() go(t, 1) go(t, 2) run() assert output == [(1,), (2,)]
def test_readers(num): m = RWMutex() clocked = makechan() cunlock = makechan() cdone = makechan() for i in range(num): go(reader, m, clocked, cunlock, cdone) for i in range(num): clocked.recv() for i in range(num): cunlock.send(True) for i in range(num): cdone.recv()
def main(): m = Mutex() c = Cond(m) n = 100 running = makechan(n) awake = makechan(n) for i in range(n): go(test, i, m, c, running, awake) if i > 0: a = awake.recv() assert a == (i - 1), "wrong coroutine woke up: want %d, got %d" % (i-1, a) running.recv() with m: c.signal()
def test_construction(): output = [] def print_(*args): output.append(args) def aCallable(value): print_("aCallable:", value) go(aCallable, 'Inline using setup') run() assert output == [("aCallable:", 'Inline using setup')] del output[:] go(aCallable, 'Inline using ()') run() assert output == [("aCallable:", 'Inline using ()')]
def test_multiple_sleep(): T1 = 0.4 * SECOND T2 = 0.1 * SECOND r1 = [] def f1(): sleep(T1) r1.append(nanotime()) r2 = [] def f2(): sleep(T2) r2.append(nanotime()) go(f1) go(f2) now = nanotime() run() assert r1[0] > r2[0] assert (now + T1 - DELTA0) <= r1[0] <= (now + T1 + DELTA), r1[0] assert (now + T2 - DELTA0) <= r2[0] <= (now + T2 + DELTA), r2[0]
def test_waitgroup(wg1, wg2): n = 16 wg1.add(n) wg2.add(n) exited = makechan(n) def f(i): wg1.done() wg2.wait() exited.send(True) for i in range(n): go(f, i) wg1.wait() for i in range(n): ret = select(exited.if_recv(), default) assert ret != exited.if_recv(), "WaitGroup released group too soon" wg2.done() for i in range(16): exited.recv()
def main(): m = Mutex() c = Cond(m) n = 2 running = makechan(n) awake = makechan(n) for i in range(n): go(test, m, c, running, awake) for i in range(n): running.recv() while n > 0: ret = select(awake.if_recv(), default) assert ret != awake.if_recv(), "coroutine not asleep" m.lock() c.signal() awake.recv() ret = select(awake.if_recv(), default) assert ret != awake.if_recv(), "too many coroutines awakes" n -= 1 c.signal()
def hammer_rwmutex(num_readers, num_iterations): activity = AtomicLong(0) rwm = RWMutex() cdone = makechan() go(writer, rwm, num_iterations, activity, cdone) for i in range(int(num_readers / 2)): go(reader, rwm, num_iterations, activity, cdone) go(writer, rwm, num_iterations, activity, cdone) for i in range(num_readers): go(reader, rwm, num_iterations, activity, cdone) for i in range(2 + num_readers): cdone.recv()
def main(): go(say, "world") say("hello")
def main(): go(Loop, 1) go(Loop, 2)
def main(): rlist.append('m') cg = go(g) cf = go(f) gosched() rlist.append('m')
def main(): go(f, 1) go(f, 2)
def main(): chan = makechan() go(exp_recv, chan) go(exp_sender, chan)
def main(): X, Y = makechan(), makechan() go(pipe, X, Y) X.send(42) assert Y.recv() == 42
def main(): ch = makechan() for each in numbers: go(counter, each, ch) for each in numbers: rlist.append(ch.recv())
def main(): c = makechan() go(f, c) go(f1, c) go(f2, c)
def main(): ch = makechan() go(sending, ch) go(receiving, ch)