def test_pool_close(self): pool = Pool(2) res = pool.map(f, [1, 2, 3]) assert res == [1, 4, 9] pool.close() with pytest.raises(ValueError): pool.map(f, [1, 2, 3]) pool.join()
def test_pi_estimation(self): pool = Pool(processes=4) NUM_SAMPLES = int(1e6) pi = 4.0 * sum(pool.map(is_inside, range(0, NUM_SAMPLES))) / NUM_SAMPLES assert 3 < pi and pi < 4 print("Pi is roughly {}".format(pi)) pool.terminate() pool.join()
def test_pool_more(self): pool = Pool(4) res = pool.map(f, [i for i in range(1000)]) pool.wait_until_workers_up() pool.terminate() pool.join() assert res == [i**2 for i in range(1000)]
def test_pool_more(self): pool = Pool(4) # explicitly start workers instead of lazy start pool.start_workers() res = pool.map(f, [i for i in range(1000)]) pool.wait_until_workers_up() pool.terminate() pool.join() assert res == [i**2 for i in range(1000)]
def test_pool_multiple_workers_inside_one_job(self): old_val = fiber_config.cpu_per_job try: fiber_config.cpu_per_job = 2 pool = Pool(4) # explicitly start workers instead of lazy start pool.start_workers() # wait for all the workers to start pool.wait_until_workers_up() res = pool.map(get_proc_name, [i for i in range(4)], chunksize=1) pool.terminate() pool.join() res.sort() # Two `ForkProcess-1` from 2 jobs, two `ForkProcess-2` from the first job assert res == ['ForkProcess-1', 'ForkProcess-1', 'ForkProcess-2', 'ForkProcess-2'], res finally: fiber_config.cpu_per_job = old_val
def main(): pool = Pool(processes=8) pi = 4.0 * sum(pool.map(is_inside, range(0, NUM_SAMPLES))) / NUM_SAMPLES print("Pi is roughly {}".format(pi))
def test_pool_basic(self): pool = Pool(2) res = pool.map(f, [1, 2, 3]) pool.terminate() pool.join() assert res == [1, 4, 9]