def test_ctx_starmap(self): pool = CtxPool(processes=2, worker_contexts=[0, 1]) # make sure both workers will have items xy = [(i, i) for i in range(10)] result = pool.starmap(ctx_func2, xy) 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_ctx_map(self): pool = CtxPool(processes=2, worker_contexts=[0, 1]) # make sure both workers will have items x = [i for i in range(10)] result = pool.map(ctx_func, x) 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_multiple_ctx_map(self): # test whether two context pools will interfere with each other pool = CtxPool(processes=2, worker_contexts=[0, 1]) pool2 = CtxPool(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_init(self): with pytest.raises(ValueError, match="not equal to the number"): _ = CtxPool(processes=2, worker_contexts=[0, 1, 2, 3]) pool = CtxPool(processes=2, initializer=init_func, initargs=("some_args",)) pool.close() pool.join() pool = CtxPool(processes=2, worker_contexts=[0, 1]) pool.close() pool.join()