python - deadline = None 在使用 urlfetch.set_default_fetch_deadline(n) 之后

标签 python google-app-engine urlfetch

我正在使用 Python 和 Google App Engine 开发网络应用程序。

我尝试按照上一个线程中的建议在全局范围内设置默认的 URLFetch 截止日期:

https://stackoverflow.com/a/14698687/2653179

urlfetch.set_default_fetch_deadline(45)

但是它不起作用 - 当我在其中一个函数中打印它的值时:urlfetch.get_default_fetch_deadline() 为 None。

这是main.py:

from google.appengine.api import users
import webapp2
import jinja2
import random
import string
import hashlib
import CQutils
import time
import os
import httpRequests
import logging
from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(45)

...

class Del(webapp2.RequestHandler):
    def get(self):
        id = self.request.get('id')
        ext = self.request.get('ext')
        user_id = httpRequests.advance(id,ext)
        d2 = urlfetch.get_default_fetch_deadline()
        logging.debug("value of deadline = %s", d2)

在日志控制台中打印:

DEBUG    2013-09-05 07:38:21,654 main.py:427] value of deadline = None

在httpRequests.py 中被调用的函数:

def advance(id, ext=None):

    url = "http://localhost:8080/api/" + id + "/advance"

    if ext is None:
        ext = ""

    params = urllib.urlencode({'ext': ext})
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})

    if (result.status_code == 200):
        return result.content

最佳答案

我知道这是一个老问题,但最近遇到了这个问题。

该设置位于线程本地,这意味着如果您的应用程序被设置为线程安全的并且您在与设置默认截止日期的线程不同的线程中处理请求,则它可能会丢失。对我来说,解决方案是在作为中间件链的一部分的每个请求之前设置截止日期。

这没有记录,需要查看源代码才能弄明白。

关于python - deadline = None 在使用 urlfetch.set_default_fetch_deadline(n) 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18623952/

相关文章:

Python:使用 tarfile 提取但忽略目录

google-app-engine - WTForms 不适用于 Google App Engine 1.6.1 和 Python 2.7

google-app-engine - 应用引擎 : Geospatial queries under Go

google-app-engine - Google App Engine - urlFetch 地址不在 _netblocks.google.com 中

google-apps-script - 使用什么 Google Apps 脚本方法来获取重定向的 URL?

python - 从 pandas 数据框中的字符串中提取信息非常困难

python - 声音设备上的多个输入

java - Google App Engine 找不到类错误 : NoClassDefFoundError

google-apps-script - 为什么在 onOpen 触发器中调用 UrlFetchApp 失败?

python - 来自 WiFi 数据的位置