0

I am newly starting with flask and I am making a server application which will handle some requests , now for the POST request part it has a process which will take some time meaning that I will have to keep the user on the mobile app waiting till I respond to the first POST request he sent .

Is there some kind of way that I can return OK for example to the mobile app and process the data in the background ?

what I have thought so far would be something like :-

if request.method=='POST':
    #signal another process ( another python file for example) to start with parameters from the request 
    return "OK"

however I am not sure that is the best practice , any other ideas ?

EDIT

following the advice of installing celery i have downloaded and installed it on windows along with redis. i have a broker worker Celery running but i am getting the following when i try to issue a POST request:

TypeError: ExtractFeatures() takes exactly 2 arguments (1 given)

for my celera with redis configuration:

from flask import Flask
from celery import Celery

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

and for the ExtractFeatures() function:

def ExtractFeatures(JsonRecieved):
#testing for now sleep for 5 seconds to make sure code executes this 
#function parallel and finishes the request handling
time.sleep(5)
print("finished Extracting Features")

for the POST request:

@app.route('/PostPhotos',methods=['POST'])
def api_PostPhotos():
if request.method=="POST":
    ExtractFeatures.delay(request.json)
return("finished request")

my expectation would be that "finished request" be printed before "finished Extracting Features" but i get that error shown above.

1 Answers1

0

It's generally a poor idea to implement long-running processes in a web application.

The typical solution is to write a message to some type of queue (SQS, Celery, files-on-disk etc.) and implement separate worker processes to run the longer operations.

If you need to notify the user when the operation is done, you can provide data in the message for that purpose, write the outcome to another queue, and have the client process check that.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
  • i am now using celery for that exact purpose however i am still a beginner with it could you help me out ? i edited the question with what i got so far. – Essameldeen Youssef Oct 08 '17 at 00:46