def f(): yield a._start() yield b._start() while len(c.ncores) < 2: yield gen.sleep(0.01) try: e1 = Executor((c.ip, c.port), start=False, loop=loop) yield e1._start() e2 = Executor(e1.scheduler, start=False, loop=loop) yield e2._start() x = e1.submit(inc, 1) y = e2.submit(inc, 2) xx = yield x._result() yy = yield y._result() assert xx == 2 assert yy == 3 yield e1._restart() assert x.cancelled() assert y.cancelled() finally: yield a._close() yield b._close() yield e1._shutdown(fast=True) yield e2._shutdown(fast=True) c.stop()
def f(c, a, b): e1 = Executor((c.ip, c.port), start=False, loop=loop) yield e1._start() assert isinstance(e1.center, rpc) assert isinstance(e1.scheduler, Scheduler) s = Scheduler((c.ip, c.port)) yield s.sync_center() done = s.start() e2 = Executor(s, start=False, loop=loop) yield e2._start() assert isinstance(e2.center, rpc) assert isinstance(e2.scheduler, Scheduler) s.listen(8042) e3 = Executor(('127.0.0.1', s.port), start=False, loop=loop) yield e3._start() assert isinstance(e3.center, rpc) assert isinstance(e3.scheduler, rpc) s.stop() yield e1._shutdown() yield e2._shutdown() yield e3._shutdown()
def test_bad_address(): try: Executor('123.123.123.123:1234', timeout=0.1) except (IOError, gen.TimeoutError) as e: assert "connect" in str(e).lower() try: Executor('127.0.0.1:1234', timeout=0.1) except (IOError, gen.TimeoutError) as e: assert "connect" in str(e).lower()
def test_global_executors(loop): assert not _global_executor[0] with pytest.raises(ValueError): default_executor() with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: assert _global_executor == [e] assert default_executor() is e with Executor(('127.0.0.1', s['port']), loop=loop) as f: assert _global_executor == [f] assert default_executor() is f assert default_executor(e) is e assert default_executor(f) is f assert not _global_executor[0]
def test_errors_dont_block(): c = Center('127.0.0.1', 8017) w = Worker('127.0.0.2', 8018, c.ip, c.port, ncores=1) e = Executor((c.ip, c.port), start=False) @gen.coroutine def f(): c.listen(c.port) yield w._start() IOLoop.current().spawn_callback(e._go) L = [ e.submit(inc, 1), e.submit(throws, 1), e.submit(inc, 2), e.submit(throws, 2) ] i = 0 while not (L[0].status == L[2].status == 'finished'): i += 1 if i == 1000: assert False yield gen.sleep(0.01) result = yield e._gather([L[0], L[2]]) assert result == [2, 3] yield w._close() c.stop() IOLoop.current().run_sync(f)
def test_fast_kill(loop): from distributed import Nanny, rpc c = Center('127.0.0.1') c.listen(0) a = Nanny(c.ip, c.port, ncores=2, ip='127.0.0.1') b = Nanny(c.ip, c.port, ncores=2, ip='127.0.0.1') e = Executor((c.ip, c.port), start=False, loop=loop) @gen.coroutine def f(): yield a._start() yield b._start() while len(c.ncores) < 2: yield gen.sleep(0.01) yield e._start() L = e.map(sleep, range(10)) try: start = time() yield e._restart() assert time() - start < 5 assert all(x.status == 'cancelled' for x in L) x = e.submit(inc, 1) result = yield x._result() assert result == 2 finally: yield a._close() yield b._close() yield e._shutdown(fast=True) c.stop() loop.run_sync(f)
def test_directed_scatter_sync(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: futures = e.scatter([1, 2, 3], workers=[('127.0.0.1', b['port'])]) has_what = sync(loop, e.scheduler.has_what) assert len(has_what[('127.0.0.1', b['port'])]) == 3 assert len(has_what[('127.0.0.1', a['port'])]) == 0
def test_restrictions_map(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() L = e.map(inc, range(5), workers={a.ip}) yield _wait(L) assert set(a.data) == {x.key for x in L} assert not b.data for x in L: assert s.restrictions[x.key] == {a.ip} L = e.map(inc, [10, 11, 12], workers=[{a.ip}, {a.ip, b.ip}, {b.ip}]) yield _wait(L) assert s.restrictions[L[0].key] == {a.ip} assert s.restrictions[L[1].key] == {a.ip, b.ip} assert s.restrictions[L[2].key] == {b.ip} with pytest.raises(ValueError): e.map(inc, [10, 11, 12], workers=[{a.ip}]) yield e._shutdown()
def test__scatter(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() d = yield e._scatter({'y': 20}) assert isinstance(d['y'], Future) assert a.data.get('y') == 20 or b.data.get('y') == 20 assert (a.address in s.who_has['y'] or b.address in s.who_has['y']) assert s.who_has['y'] assert s.nbytes == {'y': sizeof(20)} yy = yield e._gather([d['y']]) assert yy == [20] [x] = yield e._scatter([10]) assert isinstance(x, Future) assert a.data.get(x.key) == 10 or b.data.get(x.key) == 10 xx = yield e._gather([x]) assert s.who_has[x.key] assert (a.address in s.who_has[x.key] or b.address in s.who_has[x.key]) assert s.nbytes == {'y': sizeof(20), x.key: sizeof(10)} assert xx == [10] z = e.submit(add, x, d['y']) # submit works on RemoteData result = yield z._result() assert result == 10 + 20 result = yield e._gather([z, x]) assert result == [30, 10] yield e._shutdown()
def test_Future_exception_sync(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: x = e.submit(div, 1, 0) assert isinstance(x.exception(), ZeroDivisionError) x = e.submit(div, 1, 1) assert x.exception() is None
def test_restart_sync_no_center(loop): with cluster(nanny=True) as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: x = e.submit(inc, 1) e.restart() assert x.cancelled() y = e.submit(inc, 2) assert y.result() == 3
def test_future(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() x = e.submit(inc, 10) assert str(x.key) in repr(x) assert str(x.status) in repr(x) yield e._shutdown()
def test_stress_gc(): with cluster() as (c, [a, b]): with Executor(('127.0.0.1', c['port']), delete_batch_time=0.5) as e: x = e.submit(slowinc, 1) for i in range(20): # this could be increased x = e.submit(slowinc, x) assert x.result() == 22
def test_gather_after_failed_worker(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: L = e.map(inc, range(10)) wait(L) a['proc'].terminate() result = e.gather(L) assert result == list(map(inc, range(10)))
def test_submit_after_failed_worker(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: L = e.map(inc, range(10)) wait(L) a['proc'].terminate() total = e.submit(sum, L) assert total.result() == sum(map(inc, range(10)))
def test_upload_file_exception(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() with tmp_text('myfile.py', 'syntax-error!') as fn: with pytest.raises(SyntaxError): yield e._upload_file(fn) yield e._shutdown()
def test_directed_scatter(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() yield e._scatter([1, 2, 3], workers=[a.address]) assert len(a.data) == 3 assert not b.data yield e._shutdown()
def test_fast(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: L = e.map(inc, range(100)) L2 = e.map(dec, L) L3 = e.map(add, L, L2) p = progress(L3, multi=True, complete=True, notebook=True) sync(loop, p.listen) assert set(p._last_response['all']) == {'inc', 'dec', 'add'}
def f(c, a, b): e = Executor((c.ip, c.port), start=False) IOLoop.current().spawn_callback(e._go) x = e.submit(assert_list, [1, 2, 3], z=[4, 5, 6]) result = yield x._result() assert result yield e._shutdown()
def test_get_with_error(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() dsk = {'x': (div, 1, 0), 'y': (inc, 'x')} with pytest.raises(ZeroDivisionError): y = yield e._get(dsk, 'y') yield e._shutdown()
def f(c, a, b): e = Executor((c.ip, c.port), start=False) IOLoop.current().spawn_callback(e._go) x = e.submit(randint, 0, 1000, pure=True) xx = yield x._result() f = Executor((c.ip, c.port), start=False) IOLoop.current().spawn_callback(f._go) yield f._sync_center() y = f.submit(randint, 0, 1000, pure=True) yy = yield y._result() assert xx == yy yield e._shutdown() yield f._shutdown()
def test_get_releases_data(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() [x] = yield e._get({'x': (inc, 1)}, ['x']) import gc; gc.collect() assert e.refcount['x'] == 0 yield e._shutdown()
def test_start_is_idempotent(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: e.start() e.start() e.start() x = e.submit(inc, 1) assert x.result() == 2
def test_two_consecutive_executors_share_results(s, a, b): from random import randint e = Executor((s.ip, s.port), start=False) yield e._start() x = e.submit(randint, 0, 1000, pure=True) xx = yield x._result() f = Executor((s.ip, s.port), start=False) yield f._start() y = f.submit(randint, 0, 1000, pure=True) yy = yield y._result() assert xx == yy yield e._shutdown() yield f._shutdown()
def test_stress_gc(loop): n = 100 with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: x = e.submit(slowinc, 1) for i in range(n): x = e.submit(slowinc, x) assert x.result() == n + 2
def test_remote_submit_on_Future(s, a, b): e = Executor((s.ip, s.port), start=False) yield e._start() x = e.submit(lambda x: x + 1, 1) y = e.submit(lambda x: x + 1, x) result = yield y._result() assert result == 3 yield e._shutdown()
def test_submit_errors(loop): def f(a, b, c): pass e = Executor('127.0.0.1:8787', start=False, loop=loop) with pytest.raises(TypeError): e.submit(1, 2, 3) with pytest.raises(TypeError): e.map([1, 2, 3])
def test_as_completed(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: x = e.submit(inc, 1) y = e.submit(inc, 2) z = e.submit(inc, 1) seq = as_completed([x, y, z]) assert isinstance(seq, Iterator) assert set(seq) == {x, y, z}
def f(c, a, b): e = Executor((c.ip, c.port), start=False, loop=loop) yield e._start() from time import sleep x = e.submit(sleep, 3) yield x._result() yield e._shutdown()
def test_wait_sync(loop): with cluster() as (s, [a, b]): with Executor(('127.0.0.1', s['port']), loop=loop) as e: x = e.submit(inc, 1) y = e.submit(inc, 2) done, not_done = wait([x, y]) assert done == {x, y} assert not_done == set() assert x.status == y.status == 'finished'