I am running a script that listens for webhook on my local machine using the following pattern :
from flask import Flask, request, abort
app = Flask(__name__)
@app.route("/", methods=["POST"])
def webhook():
if request.method == "POST":
code
The problem is that when hearing something I want to run a piece code that takes sometimes to execute because it has to communicate with a server using an api and I am afraid it will prevent the script from listening to more while doing so.
I am using asyincio but I'm a noob so I don't know if it will execute code "while" communicating with the server.
Using the threading module is an option but from my understanding, asyincio is meant for multithreading so there should be a way to do what I want without the need for an other module.
Is there a way to do what i want within asyincio ?
What you want is to invoke app.run
passing as an argument threaded=True. Then Flask's built-in server can run multiple invocations of your application concurrently and you will not be blocking anything. For example:
from flask import Flask, request, abort
app = Flask(__name__)
...
@app.route("/", methods=["POST"])
def webhook():
if request.method == "POST":
... # long running code
...
if __name__ == '__main__':
app.run(threaded=True)
Please note that using the built-in server via app.run
is not meant for production code. Production servers will be multithreaded.