Created
February 10, 2024 01:57
-
-
Save mseeks/8488100759063b542d8e174670c87ac5 to your computer and use it in GitHub Desktop.
Example Flask app showing how to handle a webhook with an asynchronous task using threading, returning a 200 response immediately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
from threading import Thread | |
app = Flask(__name__) | |
def async_task(data): | |
# Your async operation here, e.g., process data | |
pass | |
@app.route('/webhook', methods=['POST']) | |
def webhook(): | |
data = request.json | |
# Run async_task in a separate thread | |
Thread(target=async_task, args=(data,)).start() | |
# Immediately return a 200 response to the webhook source | |
return 'OK', 200 | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment