简体   繁体   中英

Flask : How to make call-back function non-blocking using thread

I am using flask framework to interface a text-to-speech system to web. Basically website takes input via textbox and returns a audio file. Once text is entered into text box and a button is pressed below call-back will be invoked.

def task_text2speech():

    # call to my text to speech system 
    # respond to client

In above above function step "# call to my text to speech system" will take around 30-100 seconds. During this time client browser will not have any response. I want to overcome this by starting a thread instead of blocking task_text2speech in task_text2speech and once thread is done with speech synthesis respond to client.

For me implementing threading is not a issue, but I am not aware of flask handlers required to respond to client in above scenario. Any docs or example implementation are appreciated.

A simple, low-tech way you could implement this is by returning a response to the client that indicates that the server is processing the request, and in parallel to that you start your thread to do the work. You will assign a unique ID to each worker thread.

The response page that shows the "processing" message will have an auto-redirect that kicks in after, say, 5 seconds. The redirect will be to a new Flask route that takes the worker thread ID. The view function associated with this route checks the state of the worker thread, and if that thread is still at work it responds with the same "processing" page, so that the client will redirect again in another 5 secs.

When the redirect handler from the processing page finds that the worker thread finished, it can redirect to a new page that offers the generated audio file for download.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM