python - 谷歌应用程序引擎python发送电子邮件超时

标签 python google-app-engine timeout quota deadlines

我的脚本抓取 rss 页面的内容,获取该页面中的 url,将它们保存到列表中,然后抓取每个 url 的内容,并将页面的内容通过电子邮件发送给我。一切都工作得很好,接受我无法发送列表中的每个链接。通常列表中有大约 22 个链接。我不想将多个链接的内容合并到一封电子邮件中。如果我不添加超时,我会收到这样的超出配额错误

<class 'google.appengine.runtime.apiproxy_errors.OverQuotaError'>: The API call mail.Send() required more quota than is available. 

在我添加“time.sleep(9)”以减慢速度后,它给了我这个错误。

<class 'google.appengine.runtime.DeadlineExceededError'>: 
Traceback (most recent call last):

这是我的代码..有什么想法吗?

size = len(my_tabletest)
a=2 
while a < size:
  url = my_tabletest[a].split('html</link>')[0] + "print"
  url_hhhhhh = urlfetch.fetch(url)
  my_story = url_hhhhhh.content
  my_story = my_story.split('<div class="printstory">')[1]
  my_story_subject = my_story.split('<h1>')[1]
  my_story_subject = my_story_subject.split('</h1>')[0]
  my_story =  ''.join(BeautifulSoup(my_story).findAll(text=True))
  message = mail.EmailMessage(sender="me<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ff2fadfecf0f2faeff3fefcfab1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>>",
  subject=my_story_subject)
  message.to = "Jim <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aac7cfead9c5c7cfdac6cbc9cf84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>>"
  message.body = my_story
  message.html = my_story_html
  message.send()
  time.sleep(9)
  a=a+1

最佳答案

欢迎来到 Stack Overflow!

task queue是为了解决这个问题而建立的。您可以使用 deferred library 对现有代码进行最小的更改来利用它。 :

不要调用 message.send(),而是执行如下操作:

def send_email(message):  
  message.send()

deferred.defer(send_email, message)

这将创建一批临时任务,在主请求处理程序返回后在后台发送电子邮件。当您的应用程序达到出站邮件的短期配额限制时,其中一些任务可能会在第一次尝试时失败。没关系;失败的任务将自动退出并重试,直到成功。

编辑:哦,把代码中的sleep去掉。 =)

编辑#2:您可以通过将 urlfetch 移至任务中来进一步加快速度,这样每个任务都会获取一个 URL,然后发送一封电子邮件。在一个请求处理程序中获取 22 个 URL 可能足以导致超时,与发送邮件无关。

关于python - 谷歌应用程序引擎python发送电子邮件超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5845623/

相关文章:

python - Blobstore 对于生产来说足够稳定吗?

Python设置tcp发送超时

mysqli.reconnect 是否会重新运行因超时而失败的查询?

python - Python代码在C代码中添加const关键字

python - 如何正则表达式直到最后一次出现?

java - 在 Android 上使用 Google App Engine 的先决条件是什么?

haskell - 具有超时的并发 Haskell 操作

python - 如何去除双空格并留下新行? Python

python - 在 Emacs 中获取 pdb 以使用当前 virtualenv 中的 Python 进程

google-app-engine - GAE vs S3 用于存储图像/图片