在并行中运行Python子进程。

huangapple go评论81阅读模式
英文:

Run a Python subprocess in parallel

问题

我正在编写一个用于自动化任务的Python程序。我的程序使用subprocess执行另一个程序,让我们称之为“程序1”,使用以下命令:

subprocess.call(cmd, cwd=current_working_directory, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

cmd命令执行“程序1”。但“程序1”永远不会结束(我无法更正这个问题,因为我无法访问“程序1”的源代码)。

我得到一个名为output.log的文件,随着“程序1”的执行而填充。我已经创建了一个Python脚本,每60秒读取此文件,并检测“程序1”何时完成。目的是确保当Python脚本检测到“程序1”已经完成时,它会将其终止。

我希望能够同时运行这个Python脚本和“程序1”,但我不能这样做。

我尝试了两个连续的子进程:

subprocess.call(cmd, cwd=current_working_directory, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # 执行程序1

subprocess.call(cmd2, cwd="/home/.../Documents/pythonScriptFolder", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # 执行Python脚本以验证程序1是否已终止

我尝试过使用Popen

procs = [Popen(commands[i], cwd=Lcwd[i], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for i in [0,1]]

但这不起作用。我该如何修复它?

英文:

I'm writing a Python program to automate tasks. My program executes another program, let's call it "program 1", with the command subprocess:

subprocess.call(cmd, cwd=current_working_directory,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)

The cmd command executes "program 1". But "program 1" never ends (I can't correct this because I don't have access to the source code of "program 1").

I get an output.log file that fills up as "program 1" executes. I've created a Python script that reads this file every 60 seconds and detects when "program 1" has finished. The aim is to ensure that when the Python script detects that "program 1" has finished, it kills it.

I'd like to be able to run this Python script at the same time as "program 1", but I can't.

I've tried two subprocesses in a row:

subprocess.call(cmd, cwd=current_working_directory,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) # Executes the program 1

 subprocess.call(cmd2, cwd="/home/.../Documents/pythonScriptFolder",shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) # Execute Python script to verify program 1 termination

I tried it with Popen:

procs = [Popen(commands[i],cwd=Lcwd[i],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) for i in [0,1]]

This does not work. How can I fix it?

答案1

得分: -4

Python的subprocess模块允许并行执行子进程

```python
import subprocess

def run_command(command):
    process = subprocess.Popen(command, shell=True)
    process.wait()  # 等待进程完成

# 要并行运行的命令列表
commands = [
    'command_1',
    'command_2',
    'command_3',
    # 根据需要添加更多命令
]

# 创建一个列表以存储子进程对象
processes = []

# 并行启动每个命令
for command in commands:
    process = subprocess.Popen(command, shell=True)
    processes.append(process)

# 等待所有子进程完成
for process in processes:
    process.wait()

run_command函数使用subprocess.Popen运行单个命令,为每个命令创建一个单独的子进程对象并启动它。它等待所有子进程完成。根据需要修改commands列表以包含特定的命令或脚本。shell参数被使用,但你也可以直接将单个命令行参数作为参数列表传递。根据你的具体情况按需调整法律。

英文:

Python's subprocess module enables parallel subprocess execution.

import subprocess

def run_command(command):
    process = subprocess.Popen(command, shell=True)
    process.wait()  # Wait for the process to complete

# List of commands to run in parallel
commands = [
    'command_1',
    'command_2',
    'command_3',
    # Add more commands as needed
]

# Create a list to store the subprocess objects
processes = []

# Start each command in parallel
for command in commands:
    process = subprocess.Popen(command, shell=True)
    processes.append(process)

# Wait for all subprocesses to complete
for process in processes:
    process.wait()

Therun_command function runs a single command usingsubprocess.Popen, creating a separate subprocess object for each command and starting it. It waits for all subprocesses to complete. Modify the commands list to include specific commands or scripts. The shell argument is used, but you can pass individual command- line arguments directly by passing them as a list of arguments. Acclimate the law as demanded for your specific conditions.

huangapple
  • 本文由 发表于 2023年7月3日 20:31:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定