librelist archives

« back to archive

Advice on handling request that take long time in backend

Advice on handling request that take long time in backend

From:
Chengi Liu
Date:
2013-02-15 @ 22:31
Hello folks.
 I am working on building a gui/dashboard type of thing thru which I take
input from user.. and when user press submits.. I gather response and fire
a job in back end.
What I am hoping to achive is..
When the user press submits:
and that long job is being processed, I show something like: "Your job has
been submitted succesfully) submitted
and when it is finished.. I refresh the page to take user to that page.

Here is how my route.py snippet looks like
@app.route('/',methods=['POST'])
def get_data():
    data = request.form
    for data_tuple in data:

            requests_to_gb[data_tuple] = data[data_tuple]
    flag = execute_request(requests_to_gb) <---Fires a job

    if flag:
        flash("Your request has been submitted")

    else:
        flash("request could not be completed!")
    return render_template('request_submitted.html')


But the issue is.. that line where i execute_request() takes a long time to
process.. and everything is halted untill that is finished?
How do I handle this?
And how do i automatically refresh to new page as well?
THanks

Re: [flask] Advice on handling request that take long time in backend

From:
Tark
Date:
2013-02-16 @ 01:47
Maybe in this case better to create a new thread and run it with 
requests_to_gb data?

From: Chengi Liu 
Sent: Saturday, February 16, 2013 2:31 AM
To: flask@librelist.com 
Subject: [flask] Advice on handling request that take long time in backend

Hello folks. 
I am working on building a gui/dashboard type of thing thru which I take 
input from user.. and when user press submits.. I gather response and fire
a job in back end.
What I am hoping to achive is..
When the user press submits:
and that long job is being processed, I show something like: "Your job has
been submitted succesfully) submitted
and when it is finished.. I refresh the page to take user to that page.

Here is how my route.py snippet looks like
@app.route('/',methods=['POST'])
def get_data():
    data = request.form
    for data_tuple in data:

            requests_to_gb[data_tuple] = data[data_tuple]
    flag = execute_request(requests_to_gb) <---Fires a job

    if flag:
        flash("Your request has been submitted")

    else:
        flash("request could not be completed!")
    return render_template('request_submitted.html')


But the issue is.. that line where i execute_request() takes a long time 
to process.. and everything is halted untill that is finished?
How do I handle this?
And how do i automatically refresh to new page as well?
THanks

Re: [flask] Advice on handling request that take long time in backend

From:
Spacelee
Date:
2013-02-16 @ 07:51
You could either use thread or use MQ such as RabbitMQ, which do yours jobs
background.

I suggest using MQ if you have thousands of users or jobs to do...

On Sat, Feb 16, 2013 at 9:47 AM, Tark <tark.hidden@gmail.com> wrote:

>   Maybe in this case better to create a new thread and run it with
> requests_to_gb data?
>
> *From:* Chengi Liu <chengi.liu.86@gmail.com>
>  *Sent:* Saturday, February 16, 2013 2:31 AM
> *To:* flask@librelist.com
> *Subject:* [flask] Advice on handling request that take long time in
> backend
>
>  Hello folks.
> I am working on building a gui/dashboard type of thing thru which I take
> input from user.. and when user press submits.. I gather response and fire
> a job in back end.
> What I am hoping to achive is..
> When the user press submits:
> and that long job is being processed, I show something like: "Your job has
> been submitted succesfully) submitted
> and when it is finished.. I refresh the page to take user to that page.
>
> Here is how my route.py snippet looks like
>  @app.route('/',methods=['POST'])
> def get_data():
>     data = request.form
>     for data_tuple in data:
>
>             requests_to_gb[data_tuple] = data[data_tuple]
>     flag = execute_request(requests_to_gb) <---Fires a job
>
>     if flag:
>         flash("Your request has been submitted")
>
>     else:
>         flash("request could not be completed!")
>     return render_template('request_submitted.html')
>
>
> But the issue is.. that line where i execute_request() takes a long time
> to process.. and everything is halted untill that is finished?
> How do I handle this?
> And how do i automatically refresh to new page as well?
> THanks
>



-- 
*Space Lee*

Re: [flask] Advice on handling request that take long time in backend

From:
Andy D'Arcy Jewell
Date:
2013-02-16 @ 08:39
On 16/02/13 07:51, Spacelee wrote:
> You could either use thread or use MQ such as RabbitMQ, which do yours 
> jobs background.
>
> I suggest using MQ if you have thousands of users or jobs to do...
>
>
I'd suggest launching a thread to do the work and immediately returning 
a "processing..." page to the client, with a small piece of javascript 
that calls back (via a status link) every 10th of a second or so (or 
whatever is appropriate). When the thread has done its work it posts it 
to a backend data store (sql db/memcache/cassandra/message queue etc.), 
which can then be picked up asynchronously by the status routine, via 
the link.