Thanks Niphlod, I am making progress thanks to your suggestions.
I have a strange effect: if I do
def finish_request(xxx): # It receives None as parameter, I do not know why
print "yadayada"
response.custom_commit = finish_request
My function gets called, at the end of the request, *twice* in rapid
succession. Is this expected? Maybe a web2py bug? (using commit 10c67e5,
from 4 days ago)
On Thursday, March 14, 2013 12:17:36 PM UTC+1, Niphlod wrote:
>
> as said before, for 1 there's response.custom_commit and for 2 you must
> render the template inside your function.
>
> On Thursday, March 14, 2013 11:59:26 AM UTC+1, Daniel Gonzalez wrote:
>>
>> But this has two drawbacks:
>>
>> - the decorator must be added to every controller function
>> - it gets executed before rendering the template.
>>
>> On Thursday, March 14, 2013 11:56:53 AM UTC+1, Daniel Gonzalez wrote:
>>>
>>> Are you suggesting something like this (which is what I am currently
>>> doing):
>>>
>>> In my model I define a decorator:
>>>
>>> def finish_request(fn, *args, **kwargs):
>>> def wrapped():
>>> res = fn(*args, **kwargs)
>>> do_whatever_after_the_request_has_been_processed()
>>> return res
>>> return wrapped
>>>
>>> And then in my controller:
>>>
>>> @finish_request
>>> @auth.requires_login()
>>> def index():
>>> return "Hello"
>>>
>>>
>>>
>>> On Thursday, March 14, 2013 11:48:48 AM UTC+1, Niphlod wrote:
>>>>
>>>> let's dive it into .... I don't think there's an integrated way to do
>>>> it, rather than decorating your function with something else.
>>>> Please note that as far as I know (but I may be totally wrong) you
>>>> can't actually return from a function (so it gets rendered and transferred
>>>> to the client) and continue your computation....i.e. if you have return
>>>> 'abcd' to ship 'abcd' to the client, execution stops there.
>>>> So, assuming that you are fine with, e.g.
>>>> def func():
>>>> result = 'abcd'
>>>> .....some lengthy "after callback"
>>>> return result
>>>>
>>>> I'd say it's definitely "doable".
>>>> If I remember correctly, there's only one handy shortcut available
>>>> without starting toying with decorators.... response.custom_commit that
>>>> can
>>>> be a callable and gets called at the end of every request (before
>>>> returning
>>>> the results)
>>>>
>>>> PS: if the thing to do "after callback" is fine if executed
>>>> asynchronously, you can set up a queue of things to do to be processed
>>>> afterwards.
>>>>
>>>> PS2: to kick in after the rendering you must return the compiled
>>>> template, e.g.
>>>> result = response.render(...)
>>>> ...some lengthy "after callback"
>>>> return result
>>>>
>>>> On Thursday, March 14, 2013 11:35:20 AM UTC+1, Daniel Gonzalez wrote:
>>>>>
>>>>> And (I am discovering some effects during testing):
>>>>>
>>>>> 4. Is it possible to call this function *after* the templates have
>>>>> been rendered?
>>>>>
>>>>> On Thursday, March 14, 2013 11:09:51 AM UTC+1, Daniel Gonzalez wrote:
>>>>>>
>>>>>> And:
>>>>>>
>>>>>> 3. Is it possible to call this function also for jsonrpc requests?
>>>>>>
>>>>>> On Thursday, March 14, 2013 11:00:13 AM UTC+1, Daniel Gonzalez wrote:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> For some tests that I am performing, I would like to call a function
>>>>>>> "request_processed()" (defined in a model, for example), at the end of
>>>>>>> every request.
>>>>>>>
>>>>>>> 1. Is this possible? How?
>>>>>>> 2. Is it possible to also call this function in case no controller
>>>>>>> was found to serve the request?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Daniel
>>>>>>>
>>>>>>
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.