def test_maxtasksperchild(shutdown_only): def f(args): return os.getpid() pool = Pool(5, maxtasksperchild=1) assert len(set(pool.map(f, range(20)))) == 20 pool.terminate() pool.join()
def test_initializer(shutdown_only): def init(dirname): with open(os.path.join(dirname, str(os.getpid())), "w") as f: print("hello", file=f) with tempfile.TemporaryDirectory() as dirname: num_processes = 4 pool = Pool(processes=num_processes, initializer=init, initargs=(dirname,)) assert len(os.listdir(dirname)) == 4 pool.terminate() pool.join()
def fitness_function_3(x, np_ext, np_int, m_l, m_i, m_ext, m_int): m_ext_tp = [i[0] for i in m_ext] #asignar valor de coordenada de malla externa m_ext_a_sh = [i[1] for i in m_ext] p = Pool() args = [[i, m_int, m_i, m_ext_tp, m_ext_a_sh] for i in x] total = p.map(iterateArrays, args) p.close() p.join() return np.array(total)
def test_ray_init(monkeypatch, shutdown_only): def getpid(args): return os.getpid() def check_pool_size(pool, size): args = [tuple() for _ in range(size)] assert len(set(pool.map(getpid, args))) == size # Check that starting a pool starts ray if not initialized. pool = Pool(processes=2) assert ray.is_initialized() assert int(ray.cluster_resources()["CPU"]) == 2 check_pool_size(pool, 2) pool.terminate() pool.join() ray.shutdown() # Set up the cluster id so that gcs is talking with a different # storage prefix monkeypatch.setenv("RAY_external_storage_namespace", "new_cluster") ray._raylet.Config.initialize("") # Check that starting a pool doesn't affect ray if there is a local # ray cluster running. ray.init(num_cpus=3) assert ray.is_initialized() pool = Pool(processes=2) assert int(ray.cluster_resources()["CPU"]) == 3 check_pool_size(pool, 2) pool.terminate() pool.join() ray.shutdown() # Check that trying to start a pool on an existing ray cluster throws an # error if there aren't enough CPUs for the number of processes. ray.init(num_cpus=1) assert ray.is_initialized() with pytest.raises(ValueError): Pool(processes=2) assert int(ray.cluster_resources()["CPU"]) == 1 ray.shutdown()
def test_connect_to_ray(monkeypatch, ray_start_cluster): def getpid(args): return os.getpid() def check_pool_size(pool, size): args = [tuple() for _ in range(size)] assert len(set(pool.map(getpid, args))) == size address = ray_start_cluster.address # Use different numbers of CPUs to distinguish between starting a local # ray cluster and connecting to an existing one. start_cpus = 1 # Set in fixture. init_cpus = 2 # Set up the cluster id so that gcs is talking with a different # storage prefix monkeypatch.setenv("RAY_external_storage_namespace", "new_cluster") ray._raylet.Config.initialize("") # Check that starting a pool still starts ray if RAY_ADDRESS not set. pool = Pool(processes=init_cpus) assert ray.is_initialized() assert int(ray.cluster_resources()["CPU"]) == init_cpus check_pool_size(pool, init_cpus) pool.terminate() pool.join() ray.shutdown() # Check that starting a pool connects to a running ray cluster if # ray_address is passed in. pool = Pool(ray_address=address) assert ray.is_initialized() assert int(ray.cluster_resources()["CPU"]) == start_cpus check_pool_size(pool, start_cpus) pool.terminate() pool.join() ray.shutdown() monkeypatch.setenv("RAY_external_storage_namespace", "new_cluster2") ray._raylet.Config.initialize("") # Set RAY_ADDRESS, so pools should connect to the running ray cluster. os.environ["RAY_ADDRESS"] = address # Check that starting a pool connects to a running ray cluster if # RAY_ADDRESS is set. pool = Pool() assert ray.is_initialized() assert int(ray.cluster_resources()["CPU"]) == start_cpus check_pool_size(pool, start_cpus) pool.terminate() pool.join() ray.shutdown() # Check that trying to start a pool on an existing ray cluster throws an # error if there aren't enough CPUs for the number of processes. with pytest.raises(Exception): Pool(processes=start_cpus + 1) assert int(ray.cluster_resources()["CPU"]) == start_cpus ray.shutdown()
def test_ray_init(shutdown_only): def getpid(args): return os.getpid() def check_pool_size(pool, size): args = [tuple() for _ in range(size)] assert len(set(pool.map(getpid, args))) == size # Check that starting a pool starts ray if not initialized. pool = Pool(processes=2) assert ray.is_initialized() assert int(ray.cluster_resources()["CPU"]) == 2 check_pool_size(pool, 2) pool.terminate() pool.join() ray.shutdown() # Check that starting a pool doesn't affect ray if there is a local # ray cluster running. ray.init(num_cpus=3) assert ray.is_initialized() pool = Pool(processes=2) assert int(ray.cluster_resources()["CPU"]) == 3 check_pool_size(pool, 2) pool.terminate() pool.join() ray.shutdown() # Check that trying to start a pool on an existing ray cluster throws an # error if there aren't enough CPUs for the number of processes. ray.init(num_cpus=1) assert ray.is_initialized() with pytest.raises(ValueError): Pool(processes=2) assert int(ray.cluster_resources()["CPU"]) == 1 ray.shutdown()
def pool_4_processes(): pool = Pool(processes=4) yield pool pool.terminate() pool.join() ray.shutdown()