简体   繁体   English

如何在Linux中提交2个作业(以并行方式运行位于2个不同目录中的2个python脚本)?

[英]How to submit 2 jobs (to run 2 python scripts in parallel located in 2 different directories) in linux?

I need to run 2 different python scripts: script1 and script2 in parallel without any interaction. 我需要在没有任何交互的情况下并行运行2个不同的python脚本:script1和script2。 They are located as the following way: 通过以下方式定位它们:

dir0 contains the file jobcript.py and 2 directories named dir1, dir2. dir0包含文件jobcript.py和2个目录dir1,dir2。 dir1 contains script1 and dir2 contains script2. dir1包含script1,dir2包含script2。

The jobscript.txt has the following lines in it. jobscript.txt中包含以下几行。

#!/usr/bin/python
import subprocess

exit_code1 = subprocess.call(["python", "./dir1/script1", "-o"], shell=False)
exit_code2 = subprocess.call(["python", "./dir2/script2", "-o"], shell=False)

I ran the following command in linux: 我在linux中运行了以下命令:

$ python jobscript.py -o

But this runs in series. 但这是连续进行的。 How can I run them in parallel? 如何并行运行它们? Solution is much appreciated! 解决方案非常感谢!

You can get shell to put the process in the background for you: 您可以使用Shell将流程置于后台:

from subprocess import call
call(["python ./dir1/script1 -o &"], shell=True)
call(["python ./dir2/script2 -o &"], shell=True)

The "&" tells bash to put it in background. “&”告诉bash将其放在后台。 If you want python script to wait for result of each script, then you will need to create a thread. 如果希望python脚本等待每个脚本的结果,则需要创建一个线程。 I suspect you want to use a bash script instead of python: 我怀疑您想使用bash脚本而不是python:

#!/bin/bash
python ./dir1/script1 -o &
python ./dir2/script2 -o &

PS: Why call python scripts from python via subprocess in the first place? PS:为什么首先要通过子流程从python调用python脚本? You can just access that code directly. 您可以直接访问该代码。 If you want it to run in parall then multithreading or multiprocessing is your friend. 如果您希望它并行运行,那么多线程或多处理就是您的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:位于不同目录时,如何选择多个脚本之一作为包导入到主脚本中? - Python: How to choose one of multiple scripts as a package to import into a main script, when located in different directories? 如何在同一图像的并行容器中运行不同的python脚本 - How to run different python scripts in parallel containers from same image 如何在 linux 中与不同的参数并行运行 python 脚本? - How do I run a python script in parallel with different arguments in linux? 如何使用 Slurm/Sbatch 提交/运行多个并行作业? - How to submit/run multiple parallel jobs with Slurm/Sbatch? 如何在python中将不同的脚本提交到命令行- - How to submit different scripts to the command line in python - 如何使用asyncio在python3中运行并行作业? - How do I run parallel jobs in python3 with asyncio? 如何使用 Python 从不同目录运行 unitest - How to run unitest from different directories with Python 我们如何运行 2 个并行共享公共变量的 python 脚本? - How can we run 2 python scripts that share a common variable in parallel? 如何使用ProcessBuilder从Java运行并行python脚本 - How to run parallel python scripts from java using the ProcessBuilder 如何运行使用主脚本功能的脚本? (平行,python) - How to run scripts that use function of the main script? (parallel, python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM