我在这里阅读了该问题的一些答案,但没有一个对我有用。我有不同的训练文件,需要三天才能完成,但如果我同时运行这些文件,我可以减少时间。此外,当我完成所有方法的训练时,我需要直接运行测试文件。
import os
import subprocess,_multiprocessing
import sys
import gc # Garbage Collector
version = ".".join(map(str, sys.version_info[:3]))
if len(version) >3:
version=version[:-2];
current_dir = os.path.dirname(os.path.realpath(__file__))
# multiprocessing.Process(["python"+version, os.path.join(current_dir, "main_s_train.py"),os.path.join(current_dir, "SC.py")])
gc.collect()
bots = [subprocess.check_call(["python"+version, os.path.join(current_dir, "main_train.py")]),subprocess.check_call(["python"+version, os.path.join(current_dir, "main_s_train.py")]),subprocess.check_call(["python"+version, os.path.join(current_dir, "SC.py")]),subprocess.check_call(["python"+version, os.path.join(current_dir, "MC.py")])]
modules = map(__import__,bots)
import multiprocessing,subprocess
for bot in (bots):
p = multiprocessing.Process(target=lambda: __import__(bot))
p.start()
Run Code Online (Sandbox Code Playgroud)
有没有同时运行多个Python脚本的建议?
对于 Ubuntu ,这里有一个可能有效的答案。您可以通过 bash 运行多个程序。“&”告诉它在后台运行该程序。
python program1.py &
python program2.py &
Run Code Online (Sandbox Code Playgroud)
因为听起来您正在使用具有 Ubuntu 的远程服务器,所以我建议使用 tmux。它允许您打开多个会话,在每个会话上运行一个程序,并在关闭连接后保持它们运行。如果您需要输入/读取程序中的任何内容,它还允许您重新进入每个会话。几个月前,当我不得不做类似的事情时,我发现本指南很有帮助。
您也可以在 Ubuntu 上运行批处理文件。我不太熟悉在 Ubuntu 上运行批处理文件,但类似下面的内容应该适合您。您还可以添加while 循环、if 语句等。您通常在 shell 中输入的任何内容都可以放入批处理文件中,以自动运行程序或导航目录。
#!/bin/bash
ECHO starting training program
# without the "&", it waits for your training program to finish running
python training_program.py
ECHO training program completed
# Adding the "&" tells the programs to run in the background
# You can also use tmux instead, if you want to navigate the different programs
python program1.py &
python program2.py &
ECHO training programs running in the background
Run Code Online (Sandbox Code Playgroud)
该文件应使用“.sh”扩展名保存,然后通过 shell 运行以下命令使该文件可执行。
chmod +x your_batch_file.sh
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Windows,您可以创建一个运行所有程序的批处理文件。以下是您可以使用所选编辑器创建的文件示例:
# If you don't want to see the outputs/print of your training program,
# add @ECHO OFF to the start. If you want to see them, remove the @ECHO OFF
@ECHO OFF
# Without "start" before the script to run your training program,
# the batch file will wait until the training program finishes
python "path\to\your\training_program.py"
ECHO training program completed
# Adding "start" opens it in a new window, and processes the next line
# without waiting for the program to finish running
start python "path\to\your\program1.py"
ECHO Running program1
start python "path\to\your\program2.py"
ECHO Running program2
# Adding "PAUSE" makes the script wait for you manually type a key to continue,
# but it is not required. You can add PAUSE anywhere in the script
PAUSE
Run Code Online (Sandbox Code Playgroud)
“开始”在新窗口中运行每个程序。配置文本后,使用“.bat”扩展名保护该文件。然后您所要做的就是单击该文件来运行批处理文件,这将在单独的窗口中打开每个程序。
同样,您只需从命令提示符运行以下命令,它也会在单独的窗口中打开它们。
start python path\to\your\program1.py
start python path\to\your\program2.py
Run Code Online (Sandbox Code Playgroud)
但听起来您不止一次执行此操作,在这种情况下,批处理文件可能更合适。
| 归档时间: |
|
| 查看次数: |
9459 次 |
| 最近记录: |