当使用多处理时,Python不能从更高的范围访问变量?

2024-04-27 03:11:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这个运行我的脚本,出于某种原因,我得到了一个“interval\u input not defined”错误,因为出于某种原因main()函数现在无法从外部访问变量

def main():
    foo = AutoGambler()
    foo.Main(intervals=intervals_input,weaponPos=weaponPos_input,gamblePos=gamblePos_input,wipePos=wipePos_input,colors="cyan")

if __name__ == "__main__":
    intervals_input = input("Repeat how often? (X for infinite, default 10)") or 10
    weaponPos_input = input("What square is your weapon on? (e.g. 5th from left, 3rd from top => '5-3')")
    gamblePos_input = input("What square is your gamble on?")
    wipePos_input = input("What square is your wipe on?")

    freeze_support()
    print("start")
    Process(target=main).start()

当我在没有多处理器的情况下正常运行main()时,情况并非如此。我如何解决这个问题,为什么它不能再访问变量


1条回答
网友
1楼 · 发布于 2024-04-27 03:11:00

您需要使用args参数明确地向新进程传递参数

Process(target=main, args=(intervals_input , weaponPos_input ,...))

当然,您需要将main更改为具有类似main(*args)的输入参数

此外,请注意,一旦通过,它们与原始版本没有任何关系

相关问题 更多 >