简体   繁体   English

Python POST 请求在等待谷歌应用引擎的超时时间后才会发送请求

[英]Python POST request doesn't send request until after waiting for duration of timeout period on google app engine

I am making a python POST request using the python-requests library我正在使用python-requests库发出 python POST 请求

import requests

# Set token, headers, data variables here

r = requests.post(f'https://api.popularwebsite.com/endpoint?access_token={token}', headers=headers, data=data)

timetaken = r.elapsed.total_seconds()

This code is taking a long time to run, and at the end, the timetaken variable is some value between 127 and 128, however the request is successful.这段代码运行时间很长,最后, timetaken变量是 127 到 128 之间的某个值,但是请求成功。 I can add some logging and in the logs, I can see the request isn't sent until after 127 seconds我可以添加一些日志记录,在日志中,我可以看到请求直到 127 秒后才发送

Logging code:记录代码:

import logging

import http.client
http.client.HTTPConnection.debuglevel = 1

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

Logs:日志:

2021-03-15 (04:02:01.522) DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.popularwebsite.com:443
2021-03-15 (04:04:08.646) send: b'{"myJsonPayload": 1234}'

Additionally, I can pass a timeout argument of n seconds to the post function.此外,我可以将 n 秒的超时参数传递给帖子 function。 This changes how long the request waits, but the program will still sit idle for n seconds before making the request.这会改变请求等待的时间,但程序在发出请求之前仍会闲置 n 秒。 At the end, timetaken > n and timetaken < n+1.最后, timetaken > n 并且timetaken < n+1。

This is in the context of a python flask application deployed on Google App Engine.这是在部署在 Google App Engine 上的 python flask 应用程序的上下文中。 Oddly, it seems the conditions that cause this issue are the combination of my server and this api.奇怪的是,导致这个问题的条件似乎是我的服务器和这个 api 的组合。 I can make requests to other endpoints without issue from my server.我可以毫无问题地从我的服务器向其他端点发出请求。 I can also run the python code locally and make requests to the problematic api from my PC without issue.我还可以在本地运行 python 代码,并从我的 PC 毫无问题地向有问题的 api 发出请求。 Some local environment issue on the server must be messing up requests for this particular url.服务器上的某些本地环境问题必须弄乱此特定 url 的请求。 This app has been making requests to this endpoint without issue for 2 years, and spontaneously started experiencing this issue a few days ago这个应用程序向这个端点发出请求已经 2 年没有问题了,并且几天前自发地开始遇到这个问题

I have tried redeploying my app onto my server, and this might fix the issue for the first request or two, but then the issue will return.我已尝试将我的应用程序重新部署到我的服务器上,这可能会解决第一个或两个请求的问题,但随后问题将返回。 I have also attempted deploying a previous version of the code, to no avail.我还尝试部署以前版本的代码,但无济于事。 This confirms the issue is not due to a code change.这确认问题不是由于代码更改引起的。

Any ideas what could be causing this, or how I should go about troubleshooting?有什么想法可能导致这种情况,或者我应该如何 go 关于故障排除?

Edit: The issue seems sporadic.编辑:这个问题似乎是零星的。 It will start up and go away unpredictably它将启动并且 go 出乎意料地离开

Edit: Here is my app.yaml file编辑:这是我的 app.yaml 文件

runtime: python37
entrypoint: gunicorn -b :$PORT --chdir src main:app --timeout 300
instance_class: F1

env_variables:
    KEY1: "value1"

序列图

It seems to be a caching issue performed by the client, Please try to clear your browser cache as I suppose that the browser was the one doing the cache-ing.这似乎是客户端执行的缓存问题,请尝试清除浏览器缓存,因为我认为浏览器是进行缓存的浏览器。

Please note re-deploying a new version of the app will not remove the cache.请注意,重新部署应用程序的新版本不会删除缓存。 So you can use the default_expiration element of Google App Engine to configure the time a static resource will be cached in web proxies and browsers.因此,您可以使用Google App Enginedefault_expiration元素来配置 static 资源在 web 代理和浏览器中缓存的时间。

In most cases, the default 10-minute expiration time is appropriate.”在大多数情况下,默认的 10 分钟到期时间是合适的。”

If you would like caching to be refreshed quicker, then you can override it to a lower time (down to 0) by adding the following.yaml element :如果您希望更快地刷新缓存,则可以通过添加以下元素将其覆盖到更短的时间(降至 0)。yaml 元素

default_expiration: 0s. 

That will override the global expiration time, or if you wish to only set it for certain handlers, you can use the following .这将覆盖全局过期时间,或者如果您只想为某些处理程序设置它,您可以使用以下.

I wonder which app engine are you using.我想知道您使用的是哪个应用程序引擎。 The app engine flexible or standard应用引擎灵活标准

If it's standard and if there is no load then it Scales to zero and after your next call, it took time.如果它是标准的并且没有负载,那么它会缩放到零,并且在您下次调用之后,它需要时间。 But if it's flexible then a minimum of 1 instance would be there.但如果它很灵活,那么至少会有 1 个实例。 If that is not the case you have to check other reasons.如果不是这种情况,您必须检查其他原因。

If that is the case: One workaround you can set min instance for app engine standard如果是这种情况:一种解决方法,您可以为应用引擎标准设置最小实例

比较高级功能

I gave up trying to troubleshoot, and created a new GCP project in a different region and migrated.我放弃了尝试进行故障排除,并在不同的区域创建了一个新的 GCP 项目并进行了迁移。 No more issues没有更多的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM