即使我添加 'try except',读取超时仍会出现消息错误

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)

里面my_code.py有一个:

response = requests.get(url, headers=headers, timeout=5).json()

为了不存在终止终端或显示错误消息的风险,我添加了 try except,认为这是不会显示错误并继续循环下一个调用的方式。

但是这条消息(连同其他几行写的东西)出现了:

requests.exceptions.ReadTimeout: 
HTTPSConnectionPool(host='XXXXXXXXXX.com', port=443): 
Read timed out. (read timeout=5)

当它达到 5 second 限制时,我应该如何处理才不会发生这种情况,因为 try except 并没有解决问题?

详细,我不想在my_code.py中的request一起加一个try except,我很想知道有没有办法保护我的运行 这些错误的终端,但只是弄乱了我发布的这段代码。

基于以下信息: https://docs.python.org/3/library/subprocess.html#older-high-level-api

您应该将 subprocess.DEVNULL 作为 stderr 参数的值传递:

import subprocess
subprocess.call(
    "my_code.py",
    shell=True,
    stderr=subprocess.DEVNULL,
    )