Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Heroku计划的Python脚本每隔几天就会崩溃一次_Python_Heroku - Fatal编程技术网

Heroku计划的Python脚本每隔几天就会崩溃一次

Heroku计划的Python脚本每隔几天就会崩溃一次,python,heroku,Python,Heroku,我有一个脚本,每天在heroku上运行16个小时。在大多数情况下,它工作得很好,但偶尔会因为这个错误而崩溃。我一直无法解释原因。如有任何见解,将不胜感激。谢谢 2021-04-28T18:44:48.341436+00:00 app[scheduler.8000]: return self.request('GET', url, **kwargs) 2021-04-28T18:44:48.341527+00:00 app[scheduler.8000]: File "/ap

我有一个脚本,每天在heroku上运行16个小时。在大多数情况下,它工作得很好,但偶尔会因为这个错误而崩溃。我一直无法解释原因。如有任何见解,将不胜感激。谢谢

2021-04-28T18:44:48.341436+00:00 app[scheduler.8000]:     return self.request('GET', url, **kwargs)
2021-04-28T18:44:48.341527+00:00 app[scheduler.8000]:   File "/app/.heroku/python/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
2021-04-28T18:44:48.342196+00:00 app[scheduler.8000]:     resp = self.send(prep, **send_kwargs)
2021-04-28T18:44:48.342230+00:00 app[scheduler.8000]:   File "/app/.heroku/python/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
2021-04-28T18:44:48.342942+00:00 app[scheduler.8000]:     r = adapter.send(request, **kwargs)
2021-04-28T18:44:48.342974+00:00 app[scheduler.8000]:   File "/app/.heroku/python/lib/python3.9/site-packages/requests/adapters.py", line 498, in send
2021-04-28T18:44:48.343545+00:00 app[scheduler.8000]:     raise ConnectionError(err, request=request)
2021-04-28T18:44:48.343756+00:00 app[scheduler.8000]: requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
登录需要两个步骤,以下是post和get过程:

with requests.Session() as s:
  def connect():
    r = s.post(DEFAULT_LOGIN_URL, headers=default_login_headers, data=default_data)
    time.sleep(2)
    r = s.post(LOGIN_URL, headers=login_headers, data=login_data)
    time.sleep(2)
  
  def get_page():
    # failing to get the page usually means the user was logged out
    try:
      page = s.get(URL, headers=headers)
      time.sleep(2)
      soup = BeautifulSoup(page.content, "html.parser")
      # other stuff done with the page content
    except:
      connect()

  connect()
  while(true):
    get_page()
    time.sleep(60)
requirements.txt:

arrow==1.0.3     
astroid==2.5.1     
beautifulsoup4==4.9.3     
binaryornot==0.4.4     
bs4==0.0.1     
certifi==2020.12.5 
chardet==4.0.0     
click==7.1.2     
colorama==0.4.4     
cookiecutter==1.7.2     
cookiejar==0.0.3     
cycler==0.10.0    
dateparser==1.0.0
decorator==4.0.11
dnspython==2.1.0
html5lib==1.1
idna==2.10
isort==5.7.0
Jinja2==2.11.3
jinja2-time==0.2.0
kiwisolver==1.3.1
lazy-object-proxy==1.5.2
MarkupSafe==1.1.1
mccabe==0.6.1
mechanize==0.4.5
pager==3.3
pandas==1.2.3
Pillow==8.1.2
poyo==0.5.0
psaw==0.1.0
PyJWT==1.7.1
pylint==2.7.2
pymongo==3.11.3
pyparsing==2.4.7
python-dateutil==2.8.1
python-slugify==4.0.1
pytz==2021.1
regex==2020.11.13
requests==2.25.1
six==1.10.0
soupsieve==2.2.1
text-unidecode==1.3
times==0.7
toml==0.10.2
twilio==6.56.0
tzlocal==2.1
update-checker==0.18.0
urllib3==1.26.4
webencodings==0.5.1
websocket-client==0.58.0
wrapt==1.12.1

您发布的日志片段未显示代码中发生错误的位置。您有2个POST请求和1个GET请求。可能是他们中的任何一个。由于故障仅每隔几天发生一次,因此更可能发生在
get\u page()
中。一旦您在第1天连接并获得会话,就没有理由假设您的会话永远不会过期。添加一个签入
get_page()
以每12小时或24小时运行一次
connect()
,以便始终有一个更新的会话“连接中止”可能意味着另一台服务器正在阻塞;而不是失败的凭据。从提供的详细信息中无法得知。此外,当出现任何故障时,您正在从
get\u page()
执行
connect()
。而是记录错误,而不是一个空的
except
子句。并且不要用在
下定义函数。从
connect()
返回会话对象
s
,并在
get\u page()
中使用它。感谢您的建议。正如您可能猜到的,这是我的第一个Python项目。非常感谢您的意见。