tags:

views:

227

answers:

1

I'm using Web2Py and i want to import my program simply once per session... not everytime the page is loaded. is this possible ? such as "import Client" being used on the page but only import it once per session..

+1  A: 

In web2py your models and controllers are executed, not imported. They are executed every time a request arrives. If you press the button [compile] in admin, they will be bytecode compiled and some other optimizations are performs.

If your app (in models and controllers) does "import somemodule", then the import statement is executed at every request but "somemodule" is actually imported only the first time it is executed, as you asked.

mdipierro
okay. so your saying that I may not need to change anything.. I can leave it how it is "import somemodule" on my web2py app and it will do as i asked, import/execute once per session/request. .... this module i'm using is very important to the whole app so i only wanna import it once and then pass it as a arg/variable to other function as needed...
Okay... What if I don't want it to be executed everytime a request is made.. What about only ONCE per session (once per connection from an ip address) ?? Is this possible ?
Yes and no. Files you put in the models folder are executed at every request. Period. Of course those files may contain conditional statements. You can move any code you like (include table definitions) out of models/ into modules/ and import them conditionally in any way you like. Basically if you can do it in Python you can do it in web2py. The only difference is in which folder you put the code (models or modules).Let me add that there is no significant overhead in executing all models at every request. Moreover web2py caches the bytecode compiled models in ram for speed.
mdipierro
Anyway, we are talking about models. They executed because if you define them, then you probably need them on every request. If your code contains "import xyz" where "xyz" is a third party module, the module is only imported the first time. It is NOT imported every time a page is called. web2py is based on Python and treats import in the same way as any other Python program does. If a module has already been loaded it is not reloaded.
mdipierro