Project

General

Profile

ModjyLocateCallables » History » Version 2

Alan Kennedy, 2009-03-15 11:00 PM

1 1 Alan Kennedy
h1. Writing applications with modjy.
2
3
h2(#locating_callables). How modjy locates application callable objects.
4
5
6
According to the "WSGI specification":http://www.python.org/dev/peps/pep-0333/, all WSGI applications must be python callable objects, taking a defined parameter list. Application callables can be python "functions":http://docs.python.org/ref/function.html, class instances with a "__call__()":http://docs.python.org/ref/callable-types.html method, or indeed "any callable object":http://docs.python.org/ref/calls.html.
7
8
There are two different mechanisms by which you can specify application callable objects to modjy. They are
9
10
11
 # By specifying a single importable name. This requires specifying the name fully-package-qualified name of an importable WSGI handler, e.g. *some_framework.web.handlers.WSGIHandler*. This is the preferred mechanism, and takes precedence over the other mechanism, if configuration values are provided for both import mechanisms.
12
 # By specifying a combination of three names, a *directory name*, a *file name*, and a *callable name*.
13
14
Each of these mechanisms is described below
15
16
17
18
h2(#import_mechanism). The importable name mechanism.
19
20
21
This mechanism uses a single string which specifies an importable name, which when imported should deliver a python callable object. As described above, this can be "any python callable":http://docs.python.org/ref/calls.html. The importable name is specified as a single string. However, there are some small variations on how that string is specified, depending on the nature of the callable.
22
23
24
 # *Simple callable*: if the target application is directly callable, e.g. a simple function, then you need only specify the name of the callable/function, and it will be called directly by modjy. Example: *some_framework.web.handlers.WSGIHandlerFunction*
25
 # *Instance*: if the target application is an instantiable object, e.g. a class, then it will need to be "instantiated":http://www.faqs.org/docs/diveintopython/fileinfo_instantiation.html before it can be called. To do this, supply parentheses/brackets after the name. Example: *some_framework.web.handlers.WSGIHandlerClass()*. In this case, modjy will locate the definition of the *WSGIHandlerClass* in *some_framework.web.handlers*, create an instance of that class, and use that as the application. Note that when the application instance is called to service a WSGI request, the method that will be invoked is the *__call__* method. 
26
 # *Instance method*: if you want to use a class instance to service WSGI requests, but need to specify a specific method, then add the handler method name onto the end of the importable name. Example: *some_framework.web.handlers.WSGIHandlerClass().handler*. With this example, modjy will locate the WSGIHandlerClass, as described above, create an instance of it, and then use the *handler* method of that instance as that application callable.
27
28
29
h3. The importable name mechanism and reloading
30
31
32
*Reloading is not supported with the importable name mechanism*. When the importable name mechanism is used, the value of the *reload_on_mod* parameter is *ignored*.
33
34
35
h3. The importable name mechanism and caching
36
37
38
39 2 Alan Kennedy
Caching means that the same application callable object will be re-used repeatedly, to save object creation overhead. You have the choice of whether or not to enable caching using the *cache_callables* parameter, described in the [[ModjyConfiguration|modjy configuration reference]].
40 1 Alan Kennedy
41
42
If caching is *disabled* when using the importable name mechanism, this means that a new application object will be created for every new request, when appropriate. When the application is a simple callable, as described above, then caching has no meaning, since there is no instance to be re-used. When the application is an *instantiable*, as described above, then new instances will be created for every request.
43
44
If caching is *enabled* and the application callable is a class instance, then the same instance will be reused for every single request.
45
46
47
48
h2(#dir_file_call_mechanism). The directory/filename/callable callable mechanism
49
50
51
An alternative mechanism for locating an application callable is 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.
52
53
54
 # The directory in which to look for the application's python source file.
55
 # The name of the application source file in that directory.
56
 # The name of the callable application object in that source file.
57
58
59
h3. The application directory
60
61
62
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
63
64
65 2 Alan Kennedy
 # If do not supply a value for this parameter, the default value parameter is the [[ModjyServletContextRoot|servlet context root directory]]
66
 # 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.
67 1 Alan Kennedy
68
69
h3. The application filename
70
71
72
73
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*.
74
75
76
77
h3. The callable object name
78
79
80
81
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
82
83
84
85
 # *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". 
86
 # *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.
87
88
89
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*.
90
91
92
93
h3. The application object cache
94
95
96
97
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.
98
99
100
101
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.
102
103
104
105
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).
106
107
108
109
h3. Reloading source on modification
110
111
112
113 2 Alan Kennedy
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.
114 1 Alan Kennedy
115
116
117
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.
118
119
120
121
h3. Multi-threading
122
123
124
125 2 Alan Kennedy
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.