在 Flask 中处理 webhook 响应超时的最佳方法?
Best way to handle webhook response timeouts in Flask?
我在 Google Cloud Function 上有一个 Flask 应用程序 运行,它在创建订单时从 Shopify 接收 Webhook。问题是我经常超时,这就是我的意思:
@app.route('/', methods=['POST'])
def connectToSheets(request):
print('Webhook received...')
# Verify request is coming from Shopify
data = request.data
hmac_header = request.headers.get('X-Shopify-Hmac-SHA256')
verify_webhook(data, hmac_header)
print('Request validated...')
# Do some stuff...
Shopify 的 docs 指出订阅有 5 秒的超时期限和重试期限。在我验证请求后,有相当多的代码,所以我几乎每次都超时。
在验证 Webhook 之后和开始处理 Webhook 之前,有没有办法可以向 Shopify 发送 200 状态代码?或者有解决方法吗?
我曾经遇到过和你一样的问题。因此,我们使用 celery 和 rabbitMq 将处理代码从内联执行转移到在后台任务中执行。 RabbitMq 用于队列管理。您也可以使用 Redis 进行队列管理。
芹菜 - https://docs.celeryproject.org/en/stable/getting-started/index.html
RabbitMq - https://www.rabbitmq.com/documentation.html
使用 Flask、Redis 和 Celery 的异步任务 - https://stackabuse.com/asynchronous-tasks-using-flask-redis-and-celery/
如何使用 Celery 和 RabbitMQ 设置任务队列 - https://www.linode.com/docs/development/python/task-queue-celery-rabbitmq/
完全做到这一点的一种方法 w/in Cloud Functions 是设置两个函数:
- 处理初始请求的
- 第二个进行处理然后跟进响应
除了处理初始请求外,第一个函数还通过云调用第二个函数Pub/Sub。
有关完整示例,请参阅 https://dev.to/googlecloud/getting-around-api-timeouts-with-cloud-functions-and-cloud-pub-sub-47o3(这使用 Slack 的 webhook,但行为应该类似)。
我在 Google Cloud Function 上有一个 Flask 应用程序 运行,它在创建订单时从 Shopify 接收 Webhook。问题是我经常超时,这就是我的意思:
@app.route('/', methods=['POST'])
def connectToSheets(request):
print('Webhook received...')
# Verify request is coming from Shopify
data = request.data
hmac_header = request.headers.get('X-Shopify-Hmac-SHA256')
verify_webhook(data, hmac_header)
print('Request validated...')
# Do some stuff...
Shopify 的 docs 指出订阅有 5 秒的超时期限和重试期限。在我验证请求后,有相当多的代码,所以我几乎每次都超时。
在验证 Webhook 之后和开始处理 Webhook 之前,有没有办法可以向 Shopify 发送 200 状态代码?或者有解决方法吗?
我曾经遇到过和你一样的问题。因此,我们使用 celery 和 rabbitMq 将处理代码从内联执行转移到在后台任务中执行。 RabbitMq 用于队列管理。您也可以使用 Redis 进行队列管理。
芹菜 - https://docs.celeryproject.org/en/stable/getting-started/index.html
RabbitMq - https://www.rabbitmq.com/documentation.html
使用 Flask、Redis 和 Celery 的异步任务 - https://stackabuse.com/asynchronous-tasks-using-flask-redis-and-celery/
如何使用 Celery 和 RabbitMQ 设置任务队列 - https://www.linode.com/docs/development/python/task-queue-celery-rabbitmq/
完全做到这一点的一种方法 w/in Cloud Functions 是设置两个函数:
- 处理初始请求的
- 第二个进行处理然后跟进响应
除了处理初始请求外,第一个函数还通过云调用第二个函数Pub/Sub。
有关完整示例,请参阅 https://dev.to/googlecloud/getting-around-api-timeouts-with-cloud-functions-and-cloud-pub-sub-47o3(这使用 Slack 的 webhook,但行为应该类似)。