deadline = None after using urlfetch.set_default_fetch_deadline(n)

一个人想着一个人 提交于 2019-12-08 04:32:29

问题


I'm working on a web application with Python and Google App Engine.

I tried to set the default URLFetch deadline globally as suggested in a previous thread:

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

urlfetch.set_default_fetch_deadline(45)

However it doesn't work - When I print its value in one of the functions: urlfetch.get_default_fetch_deadline() is None.

Here is 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)

Prints in the Log console:

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

The function which is being called in 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

回答1:


I know this is an old question, but recently ran into the issue.

The setting is placed into a thread-local, meaning that if your application is set to thread-safe and you handle a request in a different thread than the one you set the default deadline for, it can be lost. For me, the solution was to set the deadline before every request as part of the middleware chain.

This is not documented, and required looking through the source to figure it out.



来源:https://stackoverflow.com/questions/18623952/deadline-none-after-using-urlfetch-set-default-fetch-deadlinen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!