M
M
Michael2021-02-22 10:44:17
Flask
Michael, 2021-02-22 10:44:17

Flask how to give a response before the end of processing?

Hello everyone, I’ll say right away that I haven’t worked with Flask before, but the task is too simple to do it on something complex, in general it is enough.

So the essence of the situation is this: there is a remote server (crm system), during the script, it sends a POST request to my server, which must process its logic, but is not obliged to respond. However, CRM itself believes that the answer should be, and sends several packets until the server answers at least something. This leads to the accumulation of the same data in the buffer, and actually breaks the logic of the entire script.

Please tell me if it is possible to somehow implement a quick response but continue executing the code. I read that flask is not capable of asynchronous code, so I decided to write here.

Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2021-02-22
@moonz

I found the solution myself (I just didn't formulate the question correctly)

import time
from threading import Thread
from flask import request, Flask
app = Flask(__name__)


class Compute(Thread):
    def __init__(self, request):
        Thread.__init__(self)
        self.request = request

    def run(self):
        print("start")
        time.sleep(5)
        print(self.request)
        print("done")


@app.route('/myfunc', methods=["GET", "POST"])
def myfunc():
        thread_a = Compute(request.__copy__())
        thread_a.start()
        return "Processing in background", 200

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question