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
December 18, 2024 at 12:09 am
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 subprocess
try:
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.