我正在使用 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/