Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Ray:在整个集群中循环使用附加参数的列表_Python_Ray - Fatal编程技术网

Python Ray:在整个集群中循环使用附加参数的列表

Python Ray:在整个集群中循环使用附加参数的列表,python,ray,Python,Ray,我有一个循环列表的函数: for id_ in id_list: scrape(id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path) 我可以使用以下方法并行运行它: for id_ in id_list: p1 = Process(target=scrape, args=(id_, str(id_.split('/')[-1]), id_list.index(id_),

我有一个循环列表的函数:

for id_ in id_list:
    scrape(id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path)
我可以使用以下方法并行运行它:

for id_ in id_list:
        p1 = Process(target=scrape, args=(id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path))
        p1.start()

但是,我想使用Ray并将其在集群中并行化。似乎很难让它在Ray中循环列表-我该怎么做?

我不使用
Ray
,我不知道你的问题是什么,因为你没有显示
Ray
代码和完整的错误消息,但如果我不得不使用
多处理.Pool()
要并行化,我必须首先使用
for
-loop创建包含所有参数的列表

all_args = []

for id_ in id_list:
     all_args.append( (id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path))
然后在不使用
的情况下为
-循环运行它

p = multiprocessing.Pool(10)
p.starmap(target=scrape, args=all_args)

最终,我将不得不使用列表理解在一行中完成它

p = multiprocessing.Pool(10)
p.starmap(target=scrape, args=((id_, str(id_.split('/')[-1]), id_list.index(id_) for id_ in id_lis))

我预计,
Ray
可能需要相同的方法


编辑:

带有光线的示例代码

import ray

ray.init()

@ray.remote
def scrape(id_, text, index_, length, path):
    return 'id: {} | text: {} | index: {} | len: {} | path: {}'.format(id_, text, index_, length, path)

id_list = ['2020/1', '2020/2', '2020/3', '2020/4']
target_path = '/home/user'

all_args = []

for id_ in id_list:
     all_args.append( (id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path))

futures = [scrape.remote(*args) for args in all_args]
all_results = ray.get(futures)

print(all_results)
简单的

futures = []

for id_ in id_list:
    futures.append(scrape.remote(id_, str(id_.split('/')[-1]), id_list.index(id_), len(id_list), target_path))

all_results = ray.get(futures)

可能首先创建包含所有值的列表,然后在此列表中使用
RAY
。至少对于
multiprocesing.Pool()
来说,我会这样做,因为它需要开始时的所有值。如果您对
ray
有问题,那么您应该显示
ray
代码和完整的错误消息