librelist archives

« back to archive

How can flask render a page before some task(like email sending) has not finished.

How can flask render a page before some task(like email sending) has not finished.

From:
施德来 delai
Date:
2012-06-17 @ 08:55
HI, all

Questiong:

How can flask render a page before some task(like email sending) has not
finished.

Issue:
In our case, while user submit register info to server, server will send
out a certification email first and then render a success page. But it
always take 3~5 seconds to finish the email sending, which mean i*t take at
least 3s* to get response after he submit the register infomation.

What we have done:
*1. Signal *
We have try send a signal and trigger a mail-sending function to do this
thing:
    if approved:

        # ...

        send_activation_signal.send(email)

        login_user(u)
        return redirect(url_for('register_success'))
every thing is OK except it still take *at least 3s *before
the register_success page is rendered.

*2. Fork a thread*
fork a thread to finish the email sending
        t = threading.Thread(target = send_activation, args = (email,
username, email_confirm_code, ))
        t.start()

but it looks like request context is not processed in correct way.

Exception in thread Thread-2:
> Traceback (most recent call last):
>   File
> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
> line 552, in __bootstrap_inner
>     self.run()
>   File
> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
> line 505, in run
>     self.__target(*self.__args, **self.__kwargs)
>   File "/Users/arthasshih/flask/happyvs/main_app/views/user.py", line 267,
> in send_activation
>     app.logger.debug("sending #############")
>   File
> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
> line 336, in __getattr__
>     return getattr(self._get_current_object(), name)
>   File
> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
> line 295, in _get_current_object
>     return self.__local()
>   File
> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
> line 19, in _lookup_object
>     raise RuntimeError('working outside of request context')
> RuntimeError: working outside of request context


-- 
Delai Shi 施德来

Re: [flask] How can flask render a page before some task(like email sending) has not finished.

From:
kracethekingmaker
Date:
2012-06-17 @ 09:02
Have yu tried gevent.spawn ?
> HI, all
>
> Questiong:
>
> How can flask render a page before some task(like email sending) has 
> not finished.
>
> Issue:
> In our case, while user submit register info to server, server will 
> send out a certification email first and then render a success page. 
> But it always take 3~5 seconds to finish the email sending, which mean 
> i*t take at least 3s* to get response after he submit the register 
> infomation.
>
> What we have done:
> *1. Signal *
> We have try send a signal and trigger a mail-sending function to do 
> this thing:
>     if approved:
>         # ...
>
>         send_activation_signal.send(email)
>
>         login_user(u)
>         return redirect(url_for('register_success'))
> every thing is OK except it still take *at least 3s *before 
> the register_success page is rendered.
>
> *2. Fork a thread*
> fork a thread to finish the email sending
>         t = threading.Thread(target = send_activation, args = (email, 
> username, email_confirm_code, ))
>         t.start()
>
> but it looks like request context is not processed in correct way.
>
>     Exception in thread Thread-2:
>     Traceback (most recent call last):
>       File
>     
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>     line 552, in __bootstrap_inner
>         self.run()
>       File
>     
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>     line 505, in run
>         self.__target(*self.__args, **self.__kwargs)
>       File "/Users/arthasshih/flask/happyvs/main_app/views/user.py",
>     line 267, in send_activation
>         app.logger.debug("sending #############")
>       File
>     
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>     line 336, in __getattr__
>         return getattr(self._get_current_object(), name)
>       File
>     
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>     line 295, in _get_current_object
>         return self.__local()
>       File
>     
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
>     line 19, in _lookup_object
>         raise RuntimeError('working outside of request context')
>     RuntimeError: working outside of request context
>
>
> -- 
> Delai Shi 施德来
>
>


-- 
"Talk is cheap, show me the code" -- Linus Torvalds
Regards
Kracekumar.R
www.kracekumar.com

Re: [flask] How can flask render a page before some task(like email sending) has not finished.

From:
Anthony Ford
Date:
2012-06-17 @ 09:05
I would suggest Celery, a job queue. There's even a nice flask extension:
http://docs.celeryproject.org/en/latest/index.html


Anthony Ford,
KF5IBN,
Research Assistant,
Center for Advanced Radio Astronomy,
University of Texas at Brownsville
ford.anthonyj@gmail.com



On Sun, Jun 17, 2012 at 5:02 AM, kracethekingmaker <
kracethekingmaker@gmail.com> wrote:

>  Have yu tried gevent.spawn ?
>
> HI, all
>
>  Questiong:
>
>  How can flask render a page before some task(like email sending) has not
> finished.
>
>  Issue:
> In our case, while user submit register info to server, server will send
> out a certification email first and then render a success page. But it
> always take 3~5 seconds to finish the email sending, which mean i*t take
> at least 3s* to get response after he submit the register infomation.
>
>  What we have done:
> *1. Signal *
> We have try send a signal and trigger a mail-sending function to do this
> thing:
>      if approved:
>
>         # ...
>
>          send_activation_signal.send(email)
>
>          login_user(u)
>         return redirect(url_for('register_success'))
> every thing is OK except it still take *at least 3s *before
> the register_success page is rendered.
>
>  *2. Fork a thread*
> fork a thread to finish the email sending
>          t = threading.Thread(target = send_activation, args = (email,
> username, email_confirm_code, ))
>         t.start()
>
>  but it looks like request context is not processed in correct way.
>
>  Exception in thread Thread-2:
>> Traceback (most recent call last):
>>   File
>> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>> line 552, in __bootstrap_inner
>>     self.run()
>>   File
>> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>> line 505, in run
>>     self.__target(*self.__args, **self.__kwargs)
>>   File "/Users/arthasshih/flask/happyvs/main_app/views/user.py", line
>> 267, in send_activation
>>     app.logger.debug("sending #############")
>>   File
>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>> line 336, in __getattr__
>>     return getattr(self._get_current_object(), name)
>>   File
>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>> line 295, in _get_current_object
>>     return self.__local()
>>   File
>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
>> line 19, in _lookup_object
>>     raise RuntimeError('working outside of request context')
>> RuntimeError: working outside of request context
>
>
>  --
> Delai Shi 施德来
>
>
>
>
> --
> "Talk is cheap, show me the code" -- Linus Torvalds
> Regards
> Kracekumar.Rwww.kracekumar.com
>
>

Re: [flask] How can flask render a page before some task(like email sending) has not finished.

From:
Redouane Zait
Date:
2012-06-18 @ 19:46
you need delayed jobs, check out celery as suggested earlier.
also, there's a flask extension to help you with that: falsk-celery;
https://github.com/ask/flask-celery/

On Sun, Jun 17, 2012 at 10:05 AM, Anthony Ford <ford.anthonyj@gmail.com>wrote:

> I would suggest Celery, a job queue. There's even a nice flask extension:
> http://docs.celeryproject.org/en/latest/index.html
>
>
> Anthony Ford,
> KF5IBN,
> Research Assistant,
> Center for Advanced Radio Astronomy,
> University of Texas at Brownsville
> ford.anthonyj@gmail.com
>
>
>
> On Sun, Jun 17, 2012 at 5:02 AM, kracethekingmaker <
> kracethekingmaker@gmail.com> wrote:
>
>>  Have yu tried gevent.spawn ?
>>
>> HI, all
>>
>>  Questiong:
>>
>>  How can flask render a page before some task(like email sending) has
>> not finished.
>>
>>  Issue:
>> In our case, while user submit register info to server, server will send
>> out a certification email first and then render a success page. But it
>> always take 3~5 seconds to finish the email sending, which mean i*t take
>> at least 3s* to get response after he submit the register infomation.
>>
>>  What we have done:
>> *1. Signal *
>> We have try send a signal and trigger a mail-sending function to do this
>> thing:
>>      if approved:
>>
>>         # ...
>>
>>          send_activation_signal.send(email)
>>
>>          login_user(u)
>>         return redirect(url_for('register_success'))
>> every thing is OK except it still take *at least 3s *before
>> the register_success page is rendered.
>>
>>  *2. Fork a thread*
>> fork a thread to finish the email sending
>>          t = threading.Thread(target = send_activation, args = (email,
>> username, email_confirm_code, ))
>>         t.start()
>>
>>  but it looks like request context is not processed in correct way.
>>
>>  Exception in thread Thread-2:
>>> Traceback (most recent call last):
>>>   File
>>> 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>>> line 552, in __bootstrap_inner
>>>     self.run()
>>>   File
>>> 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>>> line 505, in run
>>>     self.__target(*self.__args, **self.__kwargs)
>>>   File "/Users/arthasshih/flask/happyvs/main_app/views/user.py", line
>>> 267, in send_activation
>>>     app.logger.debug("sending #############")
>>>   File
>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>>> line 336, in __getattr__
>>>     return getattr(self._get_current_object(), name)
>>>   File
>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>>> line 295, in _get_current_object
>>>     return self.__local()
>>>   File
>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
>>> line 19, in _lookup_object
>>>     raise RuntimeError('working outside of request context')
>>> RuntimeError: working outside of request context
>>
>>
>>  --
>> Delai Shi 施德来
>>
>>
>>
>>
>> --
>> "Talk is cheap, show me the code" -- Linus Torvalds
>> Regards
>> Kracekumar.Rwww.kracekumar.com
>>
>>
>

Re: [flask] How can flask render a page before some task(like email sending) has not finished.

From:
施德来 delai
Date:
2012-06-19 @ 01:01
Thanks a lot, i will try about falsk-celery.

On Tue, Jun 19, 2012 at 3:46 AM, Redouane Zait <unrealdz@gmail.com> wrote:

> you need delayed jobs, check out celery as suggested earlier.
> also, there's a flask extension to help you with that: falsk-celery;
> https://github.com/ask/flask-celery/
>
>
> On Sun, Jun 17, 2012 at 10:05 AM, Anthony Ford <ford.anthonyj@gmail.com>wrote:
>
>> I would suggest Celery, a job queue. There's even a nice flask extension:
>> http://docs.celeryproject.org/en/latest/index.html
>>
>>
>> Anthony Ford,
>> KF5IBN,
>> Research Assistant,
>> Center for Advanced Radio Astronomy,
>> University of Texas at Brownsville
>> ford.anthonyj@gmail.com
>>
>>
>>
>> On Sun, Jun 17, 2012 at 5:02 AM, kracethekingmaker <
>> kracethekingmaker@gmail.com> wrote:
>>
>>>  Have yu tried gevent.spawn ?
>>>
>>> HI, all
>>>
>>>  Questiong:
>>>
>>>  How can flask render a page before some task(like email sending) has
>>> not finished.
>>>
>>>  Issue:
>>> In our case, while user submit register info to server, server will send
>>> out a certification email first and then render a success page. But it
>>> always take 3~5 seconds to finish the email sending, which mean i*t
>>> take at least 3s* to get response after he submit the register
>>> infomation.
>>>
>>>  What we have done:
>>> *1. Signal *
>>> We have try send a signal and trigger a mail-sending function to do this
>>> thing:
>>>      if approved:
>>>
>>>         # ...
>>>
>>>          send_activation_signal.send(email)
>>>
>>>          login_user(u)
>>>         return redirect(url_for('register_success'))
>>> every thing is OK except it still take *at least 3s *before
>>> the register_success page is rendered.
>>>
>>>  *2. Fork a thread*
>>> fork a thread to finish the email sending
>>>          t = threading.Thread(target = send_activation, args = (email,
>>> username, email_confirm_code, ))
>>>         t.start()
>>>
>>>  but it looks like request context is not processed in correct way.
>>>
>>>  Exception in thread Thread-2:
>>>> Traceback (most recent call last):
>>>>   File
>>>> 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>>>> line 552, in __bootstrap_inner
>>>>     self.run()
>>>>   File
>>>> 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
>>>> line 505, in run
>>>>     self.__target(*self.__args, **self.__kwargs)
>>>>   File "/Users/arthasshih/flask/happyvs/main_app/views/user.py", line
>>>> 267, in send_activation
>>>>     app.logger.debug("sending #############")
>>>>   File
>>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>>>> line 336, in __getattr__
>>>>     return getattr(self._get_current_object(), name)
>>>>   File
>>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
>>>> line 295, in _get_current_object
>>>>     return self.__local()
>>>>   File
>>>> 
"/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
>>>> line 19, in _lookup_object
>>>>     raise RuntimeError('working outside of request context')
>>>> RuntimeError: working outside of request context
>>>
>>>
>>>  --
>>> Delai Shi 施德来
>>>
>>>
>>>
>>>
>>> --
>>> "Talk is cheap, show me the code" -- Linus Torvalds
>>> Regards
>>> Kracekumar.Rwww.kracekumar.com
>>>
>>>
>>
>


-- 
Delai Shi 施德来