如何在Pycharm中并行运行'main'?

How to run 'main' in parallel in Pycharm?

我想 运行 我的 'main.py' 文件带有某个硬编码变量,然后更改该变量并同时再次 运行 它。代码需要一段时间 运行,所以我希望它们是并行的。

我尝试进入 'Run Configurations' 菜单并勾选允许并行 运行,但它没有任何作用。有什么办法可以做到这一点吗?

您可以为您想要的每种不同方式创建一个 运行 配置 运行 main.py 然后使用复合 运行 配置 运行 所有单独的 运行 配置。

您可以创建另一个脚本,在其中使用 subprocess.Popen(["python", "C:/filepath/main.py", "arg1", "arg2"], shell=True) 到 运行 终端上的命令,本例中的命令类似于 python C:/filepath/main.py arg1 arg2。在这里,您将参数传递给脚本,您可以通过在 main.py python 脚本中使用 import syssys.argv[1] 来访问这些参数。在这种情况下,sys.argv[1] 会给你 arg1

通过拥有两个这样的子流程,您可以开始 运行并行处理它们。示例:

a = subprocess.Popen(["python", "C:/filepath/main.py", "first_argument"], shell=True)
b = subprocess.Popen(["python", "C:/filepath/main.py", "second_argument"], shell=True)

a.wait()
b.wait()

wait() 将保持脚本 运行ning 直到两个子进程都完成。

查找 https://docs.python.org/3/library/subprocess.html 了解更多信息。