def test_starmap_async(self): pool = CtxThreadPool(processes=2, worker_contexts=[0, 1]) xy = [(i, i) for i in range(10)] result = pool.starmap_async(ctx_func2, xy).get() assert sorted([r[1] for r in result]) == [i * 2 for i in range(10)] assert {r[0] for r in result}.issubset({0, 1}) pool.close() pool.join()
def test_imap_unordered(self): pool = CtxThreadPool(processes=2, worker_contexts=[0, 1]) x = [i for i in range(10)] result = pool.imap_unordered(ctx_func, x) assert sorted([r[1] for r in result]) == x assert {r[0] for r in result}.issubset({0, 1}) pool.close() pool.join()
def test_map_async(self): pool = CtxThreadPool(processes=2, worker_contexts=[0, 1]) x = [i for i in range(10)] result = pool.map_async(ctx_func, x).get() assert sorted([r[1] for r in result]) == x assert set([r[0] for r in result]).issubset({0, 1}) pool.close() pool.join()
def test_init(self): with pytest.raises(ValueError, match="not equal to the number"): _ = CtxThreadPool(processes=2, worker_contexts=[0, 1, 2, 3]) pool = CtxThreadPool( processes=2, initializer=init_func, initargs=("some_args",) ) pool.close() pool.join() pool = CtxThreadPool(processes=2, worker_contexts=[0, 1]) pool.close() pool.join()
def test_multiple_ctx_map(self): # test whether two context thread pools will interfere with each other pool = CtxThreadPool(processes=2, worker_contexts=[0, 1]) pool2 = CtxThreadPool(processes=2, worker_contexts=[2, 3]) # create enough work items so that the execution period of two pools # will overlap x = [i for i in range(50000)] result = pool.map(ctx_func, x) result2 = pool2.map(ctx_func, x) assert sorted([r[1] for r in result]) == x assert {r[0] for r in result}.issubset({0, 1}) assert sorted([r[1] for r in result2]) == x assert {r[0] for r in result2}.issubset({2, 3}) pool.close() pool.join() pool2.close() pool2.join()
def test_apply_async(self): pool = CtxThreadPool(processes=2) assert pool.apply_async(ctx_func, (1,)).get()[1] == 1 pool.close() pool.join()
def test_apply(self): pool = CtxThreadPool(processes=2) assert pool.apply(ctx_func, (1,))[1] == 1 # for pytest-cov to run on sub processes pool.close() pool.join()