我的问题是,如果我重新分配列表中的项目,以便重新分配发生在并行进程期间,那么在并行进程完成后,更改将恢复到其原始状态。
在下面的示例中(为了便于理解而进行了极大简化),我有一个函数将列表元素 NoZeros[0] 更改为“鸡”,第二个函数将 NoZeros[1] 更改为“三明治”。我什至将“全局”放在第二个函数中,只是为了证明这不是局部与全局问题 - 它看起来像一个,但实际上不是。正如打印命令在运行程序时向您显示的那样,列表元素实际上会发生变化。问题是,当在这些过程之后调用 NoZeros 时,NoZeros 就是它的开头,而不是“[“chicken”,“sandwich”]。
我知道 python 的多处理包也有完全相同的问题,但它是通过对你不想恢复的任何内容采取“一步”并在它之前拍打“manager.list()”来解决的。我的问题是,我一生都无法弄清楚雷的等同物是什么。例如,在 Python 的多处理库中,您只需在 NoZeros 更改之前在某处编写 NoZeros=manager.list(NoZeros) 即可,这将是它的结束,但我找不到 Ray 的等效项,或者是否有Even 是等价的。
如何使用 RAY 并行更改列表?非常感谢。
另外:请注意,此脚本可能会让您陷入困境,因为您可能最终会在并行进程完成之前打印 NoZeros。这是我遇到的另一个错误,希望得到关注,但这不是优先事项。我想要说明的一点是,您可能想在下一个单元格中运行 print(NoZeros) (至少 Jupyter 有此功能)。在 python 的多处理库中,只需执行“process.join()”即可解决结束相关进程的问题,这给我带来了额外的问题:
额外问题:我如何让 ray.wait() 工作;如果前面的命令(即使这些是并行命令)完成,如何告诉我的代码仅继续执行下一个命令?
'''
import ray
ray.shutdown()
ray.init()
NoZeros=[0,0]
@ray.remote
def Chicken():
print("NoZeros[0] is",NoZeros[0],"but will change to chicken")
NoZeros[0]="chicken"
print("Now, NoZeros[0] is",NoZeros[0])
@ray.remote
def GlobalSandwich():
global NoZeros #This is just to show that "global" doesn't solve anything
print("NoZeros[1] is",NoZeros[1],"but will change to sandwich")
NoZeros[1]="sandwich"
print("Now, NoZeros[1] is",NoZeros[1])
Chicken.remote()
GlobalSandwich.remote()
#Unhash these 3 lines of code if youd like to try tackling another question: why does ray.wait() not work?
#How do i wait until parallel processes end, so i can continue my code?
#WaitList=[Chicken.remote(),GlobalSandwich.remote()]
#ray.wait(WaitList)
#print("If you see this first, ray.wait() isnt working")
#This line of code right here was executed in the next command line (Jupyter); this print command happens when the other processes are finished
print(NoZeros)
'''