Message error by read timeout keeps appearing even though I add 'try except'
import schedule
import subprocess
try:
subprocess.call("my_code.py", shell=True)
except:
pass
def trigger():
try:
subprocess.call("my_code.py", shell=True)
except:
pass
schedule.every(30).seconds.do(trigger)
while 1:
schedule.run_pending()
sleep(1)
Inside my_code.py
there is a:
response = requests.get(url, headers=headers, timeout=5).json()
So that there was no risk of kill the terminal or showing an error message, I added try except
, thinking that this is how would not show errors and would continue looping the next calls.
But this message (along with several other lines with things written) appears:
requests.exceptions.ReadTimeout:
HTTPSConnectionPool(host='XXXXXXXXXX.com', port=443):
Read timed out. (read timeout=5)
When it reaches the 5 second
limit, how should I proceed so that it doesn't happen since with try except
it didn't solve the problem?
Detail, I didn't want to add a try except
together to the request
in my_code.py
, I really would like to know if there is any way to protect my run terminal to these errors but just messing with this code I published.
Solution 1:
Based on the information from: https://docs.python.org/3/library/subprocess.html#older-high-level-api
You should pass subprocess.DEVNULL as value for stderr agument:
import subprocess
subprocess.call(
"my_code.py",
shell=True,
stderr=subprocess.DEVNULL,
)