我可以将webapp2.RequestHandler请求异步复制到不同的url吗?

2024-05-15 08:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

对于生产流量的百分比,我希望将收到的请求复制到应用程序的不同版本。这需要异步进行,这样我就不会将客户端的服务时间加倍。在

这样做的原因是为了比较prod版本和生产候选版本生成的响应。如果他们的结果相当相似,我可以确信新版本没有破坏任何东西。(如果我对应用程序进行了功能更改,我会从这个比较中筛选出响应的必要部分。)

所以我想找一个等效的:

class Foo(webapp2.RequestHandler):
  def post(self):
    handle = make_async_call_to('http://other_service_endpoint.com/', self.request)

    # process the user's request in the usual way

    test_response = handle.get_response()

    # compare the locally-prepared response and the remote one, and log
    # the diffs

    # return the locally-prepared response to the caller

更新 谷歌.appengine.api.urlphetch被建议作为我的问题的潜在解决方案,但它在dev_appserver中是同步的,尽管它的行为符合我在生产环境中想要的方式(在调用get_response()之前请求不会发出,并且会阻塞)。公司名称:

^{pr2}$

更新2

所以,在尝试了其他一些选项之后,我找到了一种完全无阻塞请求的方法:

^{3}$

…但需要注意的一点是,urllib中的异步获取选项在dev_appserver中都不起作用。发现这一点后,我回去尝试@DanCornilescu的解决方案,发现它只在生产环境中正常工作,但在dev_appserver中却不能正常工作。在


Tags: andthetodevself版本应用程序get
1条回答
网友
1楼 · 发布于 2024-05-15 08:57:26

URL获取服务支持异步请求。来自Issuing an asynchronous request

HTTP(S) requests are synchronous by default. To issue an asynchronous request, your application must:

  1. Create a new RPC object using urlfetch.create_rpc(). This object represents your asynchronous call in subsequent method calls.
  2. Call urlfetch.make_fetch_call() to make the request. This method takes your RPC object and the request target's URL as parameters.
  3. Call the RPC object's get_result() method. This method returns the result object if the request is successful, and raises an exception if an error occurred during the request.

The following snippets demonstrate how to make a basic asynchronous request from a Python application. First, import the urlfetch library from the App Engine SDK:

from google.appengine.api import urlfetch

Next, use urlfetch to make the asynchronous request:

rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")

# ... do other things ...
try:
    result = rpc.get_result()
    if result.status_code == 200:
        text = result.content
        self.response.write(text)
    else:
        self.response.status_code = result.status_code
        logging.error("Error making RPC request")
except urlfetch.DownloadError:
    logging.error("Error fetching URL0")

注意:根据Sniggerfardimungus在问题更新中提到的实验,异步调用在开发服务器上可能无法按预期工作-被序列化而不是并发,但当部署到GAE上时,它们会这样做。就我个人而言,我还没有使用异步调用,所以我不能说。在

如果目的不是阻止等待来自生产候选应用程序的响应,您可以将原始请求和生产准备响应的副本推送到任务队列上,然后响应原始请求-具有可忽略的延迟(将任务排入队列)。在

各个任务队列的处理程序将在原始请求的关键路径之外,使用原始请求的副本向登台应用程序发出请求(异步与否,从影响生产应用程序的响应时间的角度来看并不重要),获取其响应,并将其与生产准备的响应进行比较,记录增量等。这可以很好地包装在单独的模块中,以便对生产应用程序进行最小的更改,并根据需要部署/删除。在

相关问题 更多 >