2014-10-26 91 views
1

我的腳本運行完全在谷歌應用程序引擎的本地主機,但在部署腳本顯示了雲下面的錯誤(appspot.com):

「錯誤:服務器錯誤

服務器遇到錯誤,無法完成您的請求。
請在30秒後重試。「

這裏是我的代碼:爲什麼在部署後的谷歌應用程序引擎500服務器錯誤?

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) 

我只是想通過抓取打印的網址,我可以看到在本地主機上的網址,但不上部署後應用程序引擎這說明我的錯誤。

+1

服務器上的錯誤日誌裏有什麼? – stark 2014-10-26 14:53:15

+0

@stark在錯誤日誌中向我展示了「elif self.exception: DeadlineExceededError」。 – karan 2014-10-26 15:17:48

回答

2

對於自動縮放App Engine模塊,截止時間爲60秒。在您的示例代碼中,您有兩個網址提取請求,每個請求都在一個循環中,每個請求的截止時間爲60秒。假設您沒有運行基本縮放或手動縮放實例,您可能會發現60秒後您會看到此異常。即使遠程主機上有一次超時,也會導致超出前臺期限。

This page將爲您提供不同實例縮放類型的截止日期。

但是,您可能希望使用任務隊列來幫助將工作分解爲可管理的「可重試」塊。

+0

對不起,我是新手,我該怎麼做? – karan 2014-10-27 16:26:40

+0

對[同一問題]的答案中的一些評論(http://stackoverflow.com/questions/26610208/fetching-a-lot-of-ur-s-in-python-with-google-app-engine)應該幫幫我。 – tx802 2014-10-28 22:17:09

2

Google App Engine中的每個請求都有60秒的最大硬限制,因此對於任何比這更長的事情,您將獲得DeadlineExceededError

如果您知道預先準備的時間需要更多時間,那麼您將不得不使用Tasks API,您可以在其中運行長達10分鐘的時間。最後,如果您想要更長的時間,請查看Backends API,您可以在其中運行24小時。

相關問題