Python Forum
printing contents of Jar on timeout
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing contents of Jar on timeout
#1
Hello,

I have a requirement to execute a jar, and also have a timeout mechanism around it , such that when a timeout occurs, the exeuction stops.

On my research I found that we can use the 'timeout' attribute of subprocess.call()

To replicate my usecase, I have a jar which prints a message every second for 15 seconds.
Now the python application is coded as below.

import subprocess
X1 = subprocess.run(['java', '-jar', 'JarsForPython2.jar'],timeout=15)
print('------------checks for stdout-----------------------')
print(X1.stdout.decode())
print('------------checks for stderr-----------------------')
print(X1.stdout.decode())
Everything works smooth. Now to check for the timeout, I make the changes and set timeout=7
I modify the code and handle the exception as below.

try:
    X1 = subprocess.run(['java', '-jar', 'JarsForPython2.jar'], capture_output=True, timeout=7.0)
except subprocess.TimeoutExpired as e:
    print('The execution has timedout')
else:
    print('------------checks for stdout-----------------------')
    print(X1.stdout.decode())
    print('------------checks for stderr-----------------------')
    print(X1.stderr.decode())
Now here, I get

Error:
The execution has timedout Process finished with exit code 0
I just want to have the output of the first six seconds log here on my console.
Any suggestion?
Reply
#2
if I get what you want, try

try:
    X1 = subprocess.run(['java', '-jar', 'JarsForPython2.jar'], capture_output=True, timeout=7.0)
except subprocess.TimeoutExpired as e:
    print('The execution has timedout')
finally:
    print('------------checks for stdout-----------------------')
    print(X1.stdout.decode())
    print('------------checks for stderr-----------------------')
    print(X1.stderr.decode())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FTp timeout except korenron 2 3,790 Feb-01-2022, 06:51 AM
Last Post: korenron
  printing out the contents aftre subprocess.call() Rakshan 3 2,934 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  printing a list contents without brackets in a print statement paracelx 1 2,217 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  timeout value in subprocess jonesin1974 2 5,264 Dec-01-2017, 02:18 PM
Last Post: snippsat
  AsyncSSH and timeout Standard_user 1 5,671 Nov-03-2016, 06:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020