【问题标题】:Why 500 Server Error in google app engine after deploy?为什么部署后谷歌应用引擎中出现 500 服务器错误?
【发布时间】:2014-10-26 14:49:09
【问题描述】:

我的脚本在 Google 应用引擎的本地主机上完美运行,但在部署脚本时在云 (appspot.com) 上显示以下错误:

“错误:服务器错误

服务器遇到错误,无法完成您的请求。
请在 30 秒后重试。”

这是我的代码:

import webapp2
import sys
sys.path.insert(0, 'libs')
import requests
from bs4 import *
import re
import smtplib
from google.appengine.api import urlfetch
from google.appengine import runtime

class MainHandler(webapp2.RequestHandler):
  def get(self):
    self.response.write("hello")

    #urlfetch.set_default_fetch_deadline(60)

  def spider():
    count = 1
    href = 'www.example.com'
    while count <= 2:
      new_url = href
      new_source_code = urlfetch.fetch(new_url, deadline=60)
      new_plain_text = new_source_code.content
      new_soup = BeautifulSoup(new_plain_text)
      for new_link in new_soup.find_all('table'):
        for new_link1 in new_link.find_all('a'):
          new_href = 'www.example.com' + new_link1.get('href')
          new1_url = new_href
          new1_source_code = urlfetch.fetch(new1_url, deadline=60)
          new1_plain_text = new1_source_code.content
          new1_soup = BeautifulSoup(new1_plain_text)
          for new1_link in new1_soup.find_all('tbody'):
            for new1_link1 in new1_link.find_all('a', attrs={'class': 'title'}):
              new1_title = new1_link1.string
              new1_title = new1_title.strip()
              new1_href = 'www.example.com' + new1_link1.get('href')
              self.response.write(new1_title)
              self.response.write(new1_href)
    count = count + 1

  spider()

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

我只想通过爬取打印 url,部署后我可以在 localhost 上看到 url,但在应用引擎上看不到,这显示错误。

【问题讨论】:

  • 服务器上的错误日志是什么?
  • @stark 它在错误日志中显示“elif self.exception: DeadlineExceededError”。

标签: python google-app-engine web-crawler


【解决方案1】:

对于自动缩放的 App Engine 模块,截止日期为 60 秒。在您的示例代码中,您有两个 URL Fetch 请求,每个请求都在一个循环中,每个请求的截止日期为 60 秒。假设您没有在基本扩展或手动扩展实例上运行,您可能会发现在 60 秒后看到此异常。即使远程主机上的一次超时也会导致您超过前端的最后期限。

This page 将为您提供不同实例扩展类型的截止日期。

但是,您可能希望使用任务队列来帮助将工作分解为可管理的“可重试”块。

【讨论】:

  • 对不起,我是新手,我该怎么办?
  • this same question 的答案中的一些 cmets 应该会有所帮助。
【解决方案2】:

Google App Engine 中的每个请求的最大硬性限制为 60 秒,因此任何超过此时间的请求都会收到 DeadlineExceededError

如果您知道预先请求会花费更多时间,那么您将不得不使用Tasks API,您最多可以运行 10 分钟。最后,如果您想要更长的时间,请查看Backends API,在那里您可以运行长达 24 小时。

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多