Project

General

Profile

ModjyAppExecFile » History » Version 1

Alan Kennedy, 2009-03-16 04:31 PM

1 1 Alan Kennedy
h1(#execfile_mechanism). The execfile mechanism
2
3
There are two different mechanisms by which you can specify a WSGI application to modjy. This page describes the *execfile* mechanism. The [[ModjyAppImportable|importable mechanism]] is described on a different page.
4
5
This mechanism for locating an application callable requires you to specify the full path to an application file, and the name of an application callable within that file. The full path is specified using a combination of a directory name and a filename. In order to locate WSGI application callables with this mechanism, modjy needs three pieces of information.
6
7
 # The directory in which to look for the application's python source file.
8
 # The name of the application source file in that directory.
9
 # The name of the callable application object in that source file.
10
11
h2. The application directory
12
13
Firstly, you need to specify the name of a directory containing the python file containing the source code for the application. Notes for this parameter include
14
15
 # If do not supply a value for this parameter, the default value parameter is the [[ModjyServletContextRoot|servlet context root directory]]
16
 # You can specify a directory relative to the [[ModjyServletContextRoot|servlet context root directory]] by using a *$* at the beginning of path. For example, if your application resides in a directory named *my_apps_dir*, at the same level as the *WEB-INF* directory, then specify *$/my_apps_dir* as the directory name. If the *my_apps_dir* directory lives inside the *WEB-INF* directory, then specify *$/WEB-INF/my_apps_dir* as the directory name.
17
18
h2. The application filename
19
20
Once it has located the application directory, modjy then looks for an application source file. Modjy uses the same filename for all application source files. The default value for this filename is *application.py*. You can change this value by setting the modjy configuration variable *app_filename*.
21
22
h2. The callable object name
23
24
Lastly, having loaded and executed the application source file in response to a request, modjy needs to find a python callable object within the resulting python namespace. There are two different ways to set the name of the callable used for a request. They are
25
26
 # *By configuration*. The configuration parameter *app_callable_name* can be used to set the name of the callable in the python namespace. By default, modjy always uses the value of this parameter, whose default value is *handler*. So if you don't change callable name configuration, all WSGI application objects should called "handler". 
27
 # *From the request query string*. Modjy can be configured to extract the callable name from the query string of each request. This is done by specifying the name of the query field to be used in the configuration variable *callable_query_name*. If a value is given for this parameter, it will be used to search the query string of every request for a query parameter of that name. If the named query parameter is present, its value will be used as the name of the callable object. If the named query parameter is not found, the value configured for the *app_callable_name* configuration parameter (described above) will be used instead. Searching for callable names in query parameters *is disabled by default*. Security conscious deployers will probably wish to leave it disabled, since it provides a mechanism for the client to obtain access to the application's python objects.
28
29
Now modjy knows the pathname and callable name for the application. It now needs to decide whether it needs to load and compile the application source code, to find a definition of the application object, or whether it already has a usable instance of the application object in its *cache*.
30
31
h2. The application object cache
32
33
With the directory/file/callable mechanism, you can configure modjy to cache application objects for reuse for multiple requests. Application objects are stored in a cache, keyed under (pathname, callablename). If a request arrives which maps to an application object that is in the cache, then the cached object will be reused. Thus (unless the source file has been modified and *reload_on_mod* is true) the application source file will *not* be reloaded and recompiled.
34
35
Conversely, if caching is disabled, this means that a fresh application object will be created for every new request. This will have the effect of causing the reloading and recompilation of the application source file for every request, which may be less efficient than you would like. So use this option carefully.
36
37
Lastly, note that the *cache_callables* parameter has implications for the *wsgi.run_once* environment variable. Therefore, when caching is disabled, modjy sets the value of *wsgi.run_once* to *true* (thus essentially making cache disablement almost equivalent to running as a CGI script).
38
39
h2. Reloading source on modification
40
41
With the directory/file/callable mechanism, you can configure modjy to reload application objects when their containing source file has been modified. See under *reload_on_mod* in the [[ModjyConfiguration|modjy configuration reference]] for more information.
42
43
Obviously, this parameter *only* takes effect when caching is enabled. Disabled caching means that the application source code is reloaded and recompiled for every request. If reload_on_mod is enabled, then cached objects will only be discarded when their containing source file has been modified. This is obviously a useful facility during a testing/debugging cycle. Also obviously, checking the access time on application source files comes at a small but finite resource cost for every single request, so you may want to disable it in production scenarios.
44
45
h2. Multi-threading
46
47
You can configure modjy to run your application objects in either multi-threaded or single-threaded mode. See under *multithread* in the [[ModjyConfiguration|modjy configuration reference]] for more information.