[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-17 Thread Przemysław Loesch
Anthony thank you very much for your hints. I've started to learn the 
web2py workflow from the Book and directly from the web2py source but your 
advises are priceless and save a lot of my time.
Przemek

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-16 Thread Anthony
On Friday, September 16, 2016 at 7:58:26 AM UTC-4, Przemysław Loesch wrote:
>
> Massimo thank you very much for your answer. I have one more question a 
> bit out of the topic.
> Is it a way to increase web2py efficiency when it is used only for 
> database transactions and user authorization? I don't use web2py html 
> helpers, forms, views processing etc. Complete GUI of my app is created in 
> javascript and calls web2py asynchronously just to get some data as json. 
> In these conditions is it a part of web2py which could be "turned off" eg. 
> by removing imports of unnecessary modules or by setting variables? So far 
> the speed the server responses is very good but soon I'll have much bigger 
> load and just want to be sure that server side process is as 
> straightforward as it can be.
>

There is not really much framework functionality you can turn off or forego 
in order to increase performance, but here are a few things to consider:

On a given request, if you don't need the session, you should call 
session.forget(response) in order to skip checking the session for changes. 
For a bigger benefit, you can completely disable sessions for particular 
routes by using the pattern-based re-write system and adding 
dict(web2py_disable_session=True) to any relevant routes (see this example 
).
 
Unfortunately, this doesn't work with the parameter-based rewrite system.

Whenever you fetch data using the DAL, if you don't need the .update_record 
or .delete_record methods, do .select(..., cacheable=True), which will 
slightly speed up the creation of the Row objects. More generally, you can 
specify your own custom processor via .select(..., 
processor=custom_processor), which allows you to forego the creation of a 
Rows object altogether -- instead, your custom processor receives the list 
of tuples returned by the database driver, and you can do whatever minimal 
processing you need.

You can also skip the Auth initialization code on any routes that will not 
need Auth or any auth.user data (actually, you can get auth.user data for 
logged in users directly from session.auth.user).

There's another trick to get a modest speedup -- put your controller code 
in a module, import it in a model file, and send the response directly from 
the model (see this example 
).
 
Probably only worth it on requests that are otherwise very fast (i.e., not 
involving the database).

There are, of course, many other things you can do to improve efficiency, 
such as compiling the app and caching as much as possible.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-16 Thread Anthony
On Friday, September 16, 2016 at 2:16:28 PM UTC-4, Dave S wrote:
>
>
> You're going to have at least a minimal view (generic.json, frex) to 
> provide the headers and the HTTP 200.
>

You do not need a view in order to have headers set or return an HTTP 
response -- that can all be done from a controller or even a model. 
Executing views adds overhead, so you should only use them if needed.
 

> But my understanding is that the models can be the slowest part of web2py 
> (as opposed to the backend), so using conditional models or moving extra 
> models out to the modules can be useful.
>

It's not that models in general are particularly slow, just that if you 
have many model files executing on every request, that may be adding 
unnecessary overhead if you don't really need everything being defined in 
each file on every request. All things being equal, if you execute and 
return a response from a single model file (e.g., via raise HTTP(...)), 
that should actually be a bit faster than executing and returning the same 
response from a controller file (with no model files), as the latter 
involves some additional framework code execution, including making a copy 
of the entire web2py environment.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-16 Thread Dave S


On Friday, September 16, 2016 at 4:58:26 AM UTC-7, Przemysław Loesch wrote:
>
> Massimo thank you very much for your answer. I have one more question a 
> bit out of the topic.
> Is it a way to increase web2py efficiency when it is used only for 
> database transactions and user authorization? I don't use web2py html 
> helpers, forms, views processing etc. Complete GUI of my app is created in 
> javascript and calls web2py asynchronously just to get some data as json. 
> In these conditions is it a part of web2py which could be "turned off" eg. 
> by removing imports of unnecessary modules or by setting variables? So far 
> the speed the server responses is very good but soon I'll have much bigger 
> load and just want to be sure that server side process is as 
> straightforward as it can be.
> Przemek
>

You're going to have at least a minimal view (generic.json, frex) to 
provide the headers and the HTTP 200.  But my understanding is that the 
models can be the slowest part of web2py (as opposed to the backend), so 
using conditional models or moving extra models out to the modules can be 
useful.

(Caveat:  I'm just one of the "advanced beginners", but I think I've 
captured this part of the wisdom of the Respected Regulars.)

/dps

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-16 Thread Przemysław Loesch
Massimo thank you very much for your answer. I have one more question a bit 
out of the topic.
Is it a way to increase web2py efficiency when it is used only for database 
transactions and user authorization? I don't use web2py html helpers, 
forms, views processing etc. Complete GUI of my app is created in 
javascript and calls web2py asynchronously just to get some data as json. 
In these conditions is it a part of web2py which could be "turned off" eg. 
by removing imports of unnecessary modules or by setting variables? So far 
the speed the server responses is very good but soon I'll have much bigger 
load and just want to be sure that server side process is as 
straightforward as it can be.
Przemek

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is it safe to use plain XMLHttpRequest instead of ajax() function?

2016-09-15 Thread Massimo Di Pierro
No. In fact I never use the web2py ajax() function myself. It is there for 
legacy reasons. I use jQuery.ajax() almost every time.

On Friday, 16 September 2016 00:49:04 UTC-5, Przemysław Loesch wrote:
>
> I'm wondering if using plain javascript XMLHttpRequest object instead of 
> web2py ajax() can cause any problem or might be unsafe. The reason I've 
> decided to do so is that I need to pass the server response (json object) 
> to a js function as an argument.
> My js code looks like this:
> var query = document.getElementById('my-input-field').value;
> var xhttp = new XMLHttpRequest();
> xhttp.onreadystatechange = function() {
> if (this.readyState == 4 && this.status == 200) {
> var arg = JSON.parse(this.responseText);
> processArguments(arg);
> }
> };
> xhttp.open("POST", "/myaapp/controler/function/"+query, true);
> xhttp.send();
>
> Previously I used web2py ajax() function with :eval argument and short js 
> code as the part of the response to be executed for calling js function. 
> Then I've found XHTMLHttpRequest object works as well and is even easier to 
> use when we want execute js code after server response. Now I'm just 
> curious if it has ane disadvantage comparing to web2py ajax() or jQuery 
> $.ajax().
> Thank you for your answer in advance.
> Przemek
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.