[英]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.