0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicolas B
  • 3
  • 2
  • what is the actual problem you are experiencing? that your python subprocess wont kill the "program 1"? if so then we need more details on HOW it tries to kill "program 1" – Nullman Jul 03 '23 at 12:49
  • Does this answer your question? [Non blocking subprocess.call](https://stackoverflow.com/questions/16071866/non-blocking-subprocess-call) – C.Nivs Jul 03 '23 at 13:02
  • I wanted to run two programs at the same time: an executable that generates an output.log file, and a python program that reads this file. – Nicolas B Jul 03 '23 at 13:25
  • Thank you, I need some time to understand how asyncio works, maybe this will be helpfull – Nicolas B Jul 03 '23 at 13:32
  • But "cmd" and "cmd2" are not literal(?). Did you use a literal "cmd"? On Windows, there is the literal [cmd](https://en.wikipedia.org/wiki/Cmd.exe), but "/home/" suggests otherwise. Can you clarify, please? E.g., what is literal and what isn't literal? – Peter Mortensen Jul 24 '23 at 07:48

1 Answers1

-4

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.

  • 1
    Thank you, I'm testing the program, and it seems to work. However, the python code that checks whether "program 1" is complete or not contains prints I'd like to get back. Is there anything I can add to Popen to make it display the prints from the python program? – Nicolas B Jul 03 '23 at 13:23
  • 1
    Welcome to Stack Overflow, Rajan Thakur! Both of your recent answers appear likely to be entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Jul 23 '23 at 02:02
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 23 '23 at 02:02
  • Related: *[Temporary policy: Generative AI (e.g., ChatGPT) is banned](https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned)* – Peter Mortensen Jul 24 '23 at 07:39