1
@app.route('/result/<case_id>')
def result(case_id):
     USER_FOLDER = os.path.join(UPLOAD_FOLDER + '/' + Case_ID)          
     Analysis_code.main(USER_FOLDER, Case_ID, Case_ID + '_mRNA_file.txt', Case_ID + '_lncRNA_file.txt', Case_ID + '_miRNA_file.txt')

     return render_template('test.html',case_id=Case_ID)

In my this result route, I call a function that are from another file.But when flask executed Analysis_code.main(), it didn't have enough time to run through the code.

In my Analysis_code.main(), the correlation def seemed like doesn't finish the save file job, and server return gateway timeout error back.

def main():
    correlation(PATH, CASE_ID, mRNA_file_name, lncRNA_file_name, miRNA_file_name)
    bipartite_network(PATH, CASE_ID)

Is there any way to solve this problem? I've searched about subprocess, but it seems like it's not suitable for me.

I am trying to figure out how to return the render_template and the def call from another python code can still finish in the background. Now, render_template has to wait for the function call finish so that it can return the webpage.

6
  • 2
    Why is subprocess not suitable here? Seems perfect if you don't need to return the result of the function in the response to the user. Commented Jan 29, 2018 at 13:04
  • @xgord I was wondering that can subprocess accept the argument what I want to pass? Do you have example to let me know? Commented Jan 29, 2018 at 14:18
  • I found this <goo.gl/KDuMjN> to call the file but I don't know how to pass the arguments that the function needs. Commented Jan 29, 2018 at 14:24
  • It sounds like you are asking for two things at once— you want to "return the render_template and the def call...can still finish in the background", but on the other hand "render_template has to wait for the function call finish so that it can return the webpage". Do you want to wait for the function to complete before the view function returns? Commented Jan 29, 2018 at 14:46
  • @A.Vidor No, I don't want to wait for the function to complete. Because the function has to execute for a long period time and it cause 504 Gateway timeot error. Commented Jan 29, 2018 at 15:40

1 Answer 1

0

Others have suggested using subprocess though, if you wouldn't mind adding a little bit of complexity to your project, you could use celery to handle this task asynchronously.

You can keep generally the same flask route code but move what you want to be processed asynchronously into celery task that is called with delay.

# you'll need to configure celery for flask
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])

@celery.task
def analysis(case_id):
    USER_FOLDER = os.path.join(UPLOAD_FOLDER + '/' + case_id) 
    Analysis_code.main(USER_FOLDER, case_id,
                       case_id + '_mRNA_file.txt',
                       case_id + '_lncRNA_file.txt',
                       case_id + '_miRNA_file.txt')

@app.route('/result/<case_id>')
def result(case_id):
    analysis.delay(case_id)
    return render_template('test.html',case_id=case_id)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.