We’re putting the final touches on our new badges platform. Badge issuance remains temporarily paused, but all completions are being recorded and will be fulfilled once the platform is live. Thank you for your patience.

Ansys Learning Forum Forums Discuss Simulation LS Dyna Location of results when using python or cmd to call ls-run to calculate k files Reply To: Location of results when using python or cmd to call ls-run to calculate k files

Reno Genest
Ansys Employee

Hello,

You can use the timeout= option with the subprocess.run() method. I tested it and it seems to work.

I Googled "How to set a maximum running time with subprocess.run() Python?" to find the solution.

"

You can set a maximum running time (timeout) for subprocess.run() using the timeout parameter:
Python
 
import subprocesstry:    result = subprocess.run(        ["command", "arg1", "arg2"],        timeout=5,  # Timeout in seconds        capture_output=True,        text=True    )    print("Output:", result.stdout)except subprocess.TimeoutExpired:    print("Command timed out")
 
Explanation:
  • timeout: The timeout parameter takes a float value representing the maximum number of seconds you want the process to run.
  • capture_output=True: This captures the standard output and standard error of the subprocess, making them available in result.stdout and result.stderr.
  • text=True: This decodes the output as text (if possible).
If the subprocess exceeds the timeout:
A subprocess.TimeoutExpired exception will be raised and The subprocess will be terminated."
 
https://docs.python.org/3/library/subprocess.html#subprocess.TimeoutExpired
 
Let me know how it goes.
 
 
Reno.