def test_process_pool_map(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(3) with killing(pool): assert pool.map(busy_waiting, [0.1] * 5) == [0.1] * 5 assert len(set(pool.map(get_pid_anyway, range(10)))) == 3 assert len(proc.children()) == 3 assert len(proc.children()) == 0
def test_process_pool_respawns_worker(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(2) with killing(pool): pids1 = pool.map(get_pid_anyway, range(2)) pool.kill() pids2 = pool.map(get_pid_anyway, range(2)) assert not set(pids1).intersection(pids2) assert len(proc.children()) == 0
def test_process_pool_raises(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(1) with killing(pool): pid1 = pool.spawn(os.getpid).get() g = pool.spawn(divide_by_zero) with pytest.raises(ZeroDivisionError): g.get() pid2 = pool.spawn(os.getpid).get() assert pid1 == pid2 assert len(proc.children()) == 0
def test_process_pool_apply(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(2) with killing(pool): pool.apply_async(busy_waiting, (0.2,)) pool.apply_async(busy_waiting, (0.2,)) pool.apply_async(busy_waiting, (0.2,)) with Timeout(3): pool.join() assert len(proc.children()) == 2 assert len(proc.children()) == 0
def test_process_pool_recycles_child_process(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(1) with killing(pool): pids = set() for x in range(10): pids.add(pool.spawn(os.getpid).get()) assert len(pids) == 1 assert next(iter(pids)) != os.getpid() assert len(proc.children()) == 1 assert len(proc.children()) == 0
def test_process_pool_unlimited(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool() with killing(pool): for x in range(5): pids = pool.map(get_pid_anyway, range(x + 1)) assert len(pids) == x + 1 assert len(proc.children()) == x + 1 for x in reversed(range(5)): pids = pool.map(get_pid_anyway, range(x + 1)) assert len(pids) == x + 1 assert len(proc.children()) == 5 assert len(proc.children()) == 0
def test_process_pool_waits_worker_available(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(2) with killing(pool): with Timeout(0.1): pool.spawn(busy_waiting, 0.5) pool.spawn(busy_waiting, 0.5) with pytest.raises(Timeout): with Timeout(0.1): pool.spawn(busy_waiting, 0.5) pool.join() assert len(proc.children()) == 2 assert len(proc.children()) == 0
def test_process_pool_size_limited(proc): assert len(proc.children()) == 0 pool = lets.ProcessPool(2) with killing(pool): pool.spawn(busy_waiting, 0.1) pool.spawn(busy_waiting, 0.2) pool.spawn(busy_waiting, 0.1) pool.spawn(busy_waiting, 0.2) pool.spawn(busy_waiting, 0.1) pool.join(0) assert len(proc.children()) == 2 pool.join() assert len(proc.children()) == 0