如何同时运行多个python脚本?

问题描述 投票:0回答:1

我已经阅读了关于here的一些答案,但没有一个对我有用。我有不同的培训文件,需要三天才能完成,但是如果同时运行这些文件,则可以减少时间。此外,完成所有方法的培训后,我需要直接运行测试文件。

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()

是否有建议同时运行多个python脚本。

python-3.x
1个回答
1
投票

对于Ubuntu,可能有一个答案here。您可以通过bash运行多个程序。 “&”告诉它在后台运行程序。

python program1.py &
python program2.py &

由于听起来您正在使用具有Ubuntu的远程服务器,所以我建议改用tmux。它使您可以打开多个会话,在每个会话上运行一个程序,并在关闭连接后使它们保持运行状态。如果您需要输入/读取程序中的任何内容,它还允许您重新进入每个会话。几个月前我不得不做类似的事情时,我发现此guide很有帮助。

您也可以在Ubuntu上运行批处理文件。我对在Ubuntu上运行批处理文件不太熟悉,但是类似以下的内容应该适合您。您也可以添加while loops, if statements, etc.。您通常在外壳程序中键入的任何内容都可以放入批处理文件中,以自动运行程序或导航目录。

#!/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

该文件应以“ .sh”扩展名保存,然后通过在外壳程序中运行以下命令来使该文件可执行。

chmod +x your_batch_file.sh 

[如果使用的是Windows,则可以运行所有程序的create a batch file。这是您可以使用所选编辑器创建的文件的示例:

# 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

"start"在新窗口中运行每个程序。配置文本后,请以“ .bat”扩展名保护文件。然后,您所要做的就是单击文件以运行批处理文件,这将在单独的窗口中打开每个程序。

同样,您可以从命令提示符处运行以下命令,它也会在单独的窗口中打开它们。

start python path\to\your\program1.py
start python path\to\your\program2.py

但是听起来您执行了多次,在这种情况下,批处理文件可能更合适。

© www.soinside.com 2019 - 2024. All rights reserved.