346
346
information such as the source IP of the request to the log entry.
349
\begin{funcdesc}{import_module}{module_name\optional{, autoreload=1, log=0, path=None}}
350
This function can be used to import modules taking advantage of
351
mod_python's internal mechanism which reloads modules automatically
352
if they have changed since last import.
354
\var{module_name} is a string containing the module name (it can
355
contain dots, e.g. \code{mypackage.mymodule}); \var{autoreload}
356
indicates whether the module should be reloaded if it has changed since
357
last import; when \var{log} is true, a message will be written to
358
the logs when a module is reloaded; \var{path} allows restricting
359
modules to specific paths.
349
\begin{funcdesc}{import_module}{module_name\optional{, autoreload=None, log=None, path=None}}
350
This function can be used to import modules.
353
This function and the module importer were completely reimplemented in
354
mod_python 3.3. If you are using an older version of mod_python do not
355
rely on this documentation and instead refer to the documentation for
356
the specific version you are using as the new importer does not behave
357
exactly the same and has additional features.
359
If you are trying to port code from an older version of mod_python to
360
mod_python 3.3 and can't work out why the new importer is not working
361
for you, you can enable the old module importer for specific Python
362
interpreter instances by using:
365
PythonOption mod_python.legacy.importer name
368
where 'name' is the name of the interpreter instance or '*' for it to
369
be applied to all interpreter instances. This option should be placed
370
at global context within the main Apache configuration files.
373
When using the \code{apache.import_module()} function, the
374
\var{module_name} should be a string containing either the module name,
375
or a path to the actual code file for the module; where a module is a
376
candidate for automatic module reloading, \var{autoreload} indicates
377
whether the module should be reloaded if it has changed since the last
378
import; when \var{log} is true, a message will be written to the logs
379
when a module is reloaded; \var{path} can be a list specifying additional
380
directories to be searched for modules.
382
With the introduction of mod_python 3.3, the default arguments for the
383
\var{autoreload} and \var{log} arguments have been changed to
384
\code{None}, with the arguments effectively now being unnecessary except
385
in special circumstances. When the arguments are left as the default of
386
\code{None}, the Apache configuration in scope at the time of the call
387
will always be consulted automatically for any settings for the
388
\code{PythonAutoReload} and \code{PythonDebug} directives respectively.
364
393
from mod_python import apache
365
mymodule = apache.import_module('mymodule', log=1)
394
module = apache.import_module('module_name')
397
The \code{apache.import_module()} function is not just a wrapper for the
398
standard Python module import mechanism. The purpose of the function and
399
the mod_python module importer in general, is to provide a means of being
400
able to import modules based on their exact location, with modules being
401
distinguished based on their location rather than just the name of the
402
module. Distinguishing modules in this way, rather than by name alone,
403
means that the same module name can be used for handlers and other code
404
in multiple directories and they will not interfere with each other.
406
A secondary feature of the module importer is to implement a means of
407
having modules automatically reloaded when the corresponding code file
408
has been changed on disk. Having modules be able to be reloaded in this
409
way means that it is possible to change the code for a web application
410
without having to restart the whole Apache web server. Although this was
411
always the intent of the module importer, prior to mod_python 3.3, its
412
effectiveness was limited. With mod_python 3.3 however, the module
413
reloading feature is much more robust and will correctly reload parent
414
modules even when it was only a child module what was changed.
416
When the \code{apache.import_module()} function is called with just the
417
name of the module, as opposed to a path to the actual code file for the
418
module, a search has to be made for the module. The first set of
419
directories that will be checked are those specified by the \var{path}
420
argument if supplied.
422
Where the function is called from another module which had previously
423
been imported by the mod_python importer, the next directory which will
424
be checked will be the same directory as the parent module is located.
425
Where that same parent module contains a global data variable called
426
\code{__mp_path__} containing a list of directories, those directories
427
will also be searched.
429
Finally, the mod_python module importer will search directories
430
specified by the \code{PythonOption} called \code{mod_python.importer.path}.
435
PythonOption mod_python.importer.path "['/some/path']"
438
The argument to the option must be in the form of a Python list. The
439
enclosing quotes are to ensure that Apache interprets the argument as a
440
single value. The list must be self contained and cannot reference any
441
prior value of the option. The list MUST NOT reference \code{sys.path}
442
nor should any directory which also appears in \code{sys.path} be
443
listed in the mod_python module importer search path.
445
When searching for the module, a check is made for any code file with the
446
name specified and having a '.py' extension. Because only modules
447
implemented as a single file will be found, packages will not be found
448
nor modules contained within a package.
450
In any case where a module cannot be found, control is handed off to the
451
standard Python module importer which will attempt to find the module or
452
package by searching \code{sys.path}.
454
Note that only modules found by the mod_python module importer are
455
candidates for automatic module reloading. That is, where the mod_python
456
module importer could not find a module and handed the search off to the
457
standard Python module importer, those modules or packages will not be
460
Although true Python packages are not candidates for reloading and must
461
be located in a directory listed in \code{sys.path}, another form of
462
packaging up modules such that they can be maintained within their own
463
namespace is supported. When this mechanism is used, these modules will
464
be candidates for reloading when found by the mod_python module importer.
466
In this scheme for maintaining a pseudo package, individual modules are
467
still placed into a directory, but the \code{__init__.py} file in the
468
directory has no special meaning and will not be automatically imported
469
as is the case with true Python packages. Instead, any module within the
470
directory must always be explicitly identified when performing an import.
472
To import a named module contained within these pseudo packages, rather
473
than using a '.' to distinguish a sub module from the parent, a '/' is
474
used instead. For example:
477
from mod_python import apache
478
module = apache.import_module('dirname/module_name')
481
If an \code{__init__.py} file is present and it was necessary to import
482
it to achieve the same result as importing the root of a true Python
483
package, then \code{__init__} can be used as the module name. For example:
486
from mod_python import apache
487
module = apache.import_module('dirname/__init__')
490
As a true Python package is not being used, if a module in the directory
491
needs to refer to another module in the same directory, it should use
492
just its name, it should not use any form of dotted path name via the
493
root of the package as would be the case for true Python packages.
494
Modules in subdirectories can be imported by using a '/' separated path
495
where the first part of the path is the name of the subdirectory.
497
As a new feature in mod_python 3.3, when using the standard Python
498
'import' statement to import a module, if the import is being done from a
499
module which was previously imported by the mod_python module importer,
500
it is equivalent to having called \code{apache.import_module()} directly.
511
from mod_python import apache
512
name = apache.import_module('name')
515
It is also possible to use constructs such as:
518
import name as module
524
from name import value
527
Although the 'import' statement is used, that it maps through to the
528
\code{apache.import_module()} function ensures that parent/child
529
relationships are maintained correctly and reloading of a parent will
530
still work when only the child has been changed. It also ensures that one
531
will not end up with modules which were separately imported by the
532
mod_python module importer and the standard Python module importer.
534
With the reimplementation of the module importer in mod_python 3.3, the
535
\var{module_name} argument may also now be an absolute path name of an
536
actual Python module contained in a single file. On Windows, a drive
537
letter can be supplied if necessary. For example:
540
from mod_python import apache
541
name = apache.import_module('/some/path/name.py')
547
from mod_python import apache
549
here = os.path.dirname(__file__)
550
path = os.path.join(here, 'module.py')
551
module = apache.import_module(path)
554
Where the file has an extension, that extension must be supplied. Although
555
it is recommended that code files still make use of the '.py' extension,
556
it is not actually a requirement and an alternate extension can be used.
560
from mod_python import apache
562
here = os.path.dirname(__file__)
563
path = os.path.join(here, 'servlet.mps')
564
servlet = apache.import_module(path)
567
To avoid the need to use hard coded absolute path names to modules, a few
568
shortcuts are provided. The first of these allow for the use of relative
569
path names with respect to the directory the module performing the
570
import is located within.
575
from mod_python import apache
577
parent = apache.import_module('../module.py')
578
subdir = apache.import_module('./subdir/module.py')
581
Forward slashes must always be used for the prefixes './' and '../', even
582
on Windows hosts where native pathname use a backslash. This convention
583
of using forward slashes is used as that is what Apache normalizes all
584
paths to internally. If you are using Windows and have been using
585
backward slashes with \code{Directory} directives etc, you are using
586
Apache contrary to what is the accepted norm.
588
A further shortcut allows paths to be declared relative to what is
589
regarded as the handler root directory. The handler root directory is the
590
directory context in which the active \code{Python*Handler} directive was
591
specified. If the directive was specified within a \code{Location} or
592
\code{VirtualHost} directive, or at global server scope, the handler root
593
will be the relevant document root for the server.
595
To express paths relative to the handler root, the '\textasciitilde/' prefix
596
should be used. A forward slash must again always be used, even on Windows.
601
from mod_python import apache
603
parent = apache.import_module('~/../module.py')
604
subdir = apache.import_module('~/subdir/module.py')
607
In all cases where a path to the actual code file for a module is given,
608
the \var{path} argument is redundant as there is no need to search
609
through a list of directories to find the module. In these situations,
610
the \var{path} is instead taken to be a list of directories to use as the
611
initial value of the \code{__mp_path__} variable contained in the
612
imported modules instead of an empty path.
614
This feature can be used to attach a more restrictive search path to a
615
set of modules rather than using the \code{PythonOption} to set a global
616
search path. To do this, the modules should always be imported through a
617
specific parent module. That module should then always import submodules
618
using paths and supply \code{__mp_path__} as the \var{path} argument to
619
subsequent calls to \code{apache.import_module()} within that module. For
623
from mod_python import apache
625
module1 = apache.import_module('./module1.py', path=__mp_path__)
626
module2 = apache.import_module('./module2.py', path=__mp_path__)
629
with the module being imported as:
632
from mod_python import apache
634
parent = apache.import_module('~/modules/parent.py', path=['/some/path'])
637
The parent module may if required extend the value of \code{__mp_path__}
638
prior to using it. Any such directories will be added to those inherited
639
via the \var{path} argument. For example:
642
from mod_python import apache
645
here = os.path.dirname(__file__)
646
subdir = os.path.join(here, 'subdir')
647
__mp_path__.append(subdir)
649
module1 = apache.import_module('./module1.py', path=__mp_path__)
650
module2 = apache.import_module('./module2.py', path=__mp_path__)
653
In all cases where a search path is being specified which is specific
654
to the mod_python module importer, whether it be specified using the
655
\code{PythonOption} called \code{mod_python.importer.path}, using the
656
\var{path} argument to the \code{apache.import_module()} function or
657
in the \code{__mp_path__} attribute, the prefix '\textasciitilde/'
658
can be used in a path and that path will be taken as being relative
659
to handler root. For example:
662
PythonOption mod_python.importer.path "['~/modules']"
665
If wishing to refer to the handler root directory itself, then
666
'\textasciitilde' can be used and the trailing slash left off. For example:
669
PythonOption mod_python.importer.path "['~']"
672
Note that with the new module importer, as directories associated with
673
\code{Python*Handler} directives are no longer being added automatically
674
to \code{sys.path} and they are instead used directly by the module
675
importer only when required, some existing code which expected to be able
676
to import modules in the handler root directory from a module in a
677
subdirectory may no longer work. In these situations it will be necessary
678
to set the mod_python module importer path to include '\textasciitilde'
679
or list '\textasciitilde' in the \code{__mp_path__} attribute of the module
680
performing the import.
682
This trick of listing '\textasciitilde' in the module importer path
683
will not however help in the case where Python packages were previously
684
being placed into the handler root directory. In this case, the Python
685
package should either be moved out of the document tree and the directory
686
where it is located listed against the \code{PythonPath} directive, or the
687
package converted into the pseudo packages that mod_python supports and
688
change the module imports used to access the package.
690
Only modules which could be imported by the mod_python module importer
691
will be candidates for automatic reloading when changes are made to the
692
code file on disk. Any modules or packages which were located in a
693
directory listed in \code{sys.path} and which were imported using the
694
standard Python module importer will not be candidates for reloading.
696
Even where modules are candidates for module reloading, unless a true
697
value was explicitly supplied as the \var{autoreload} option to the
698
\code{apache.import_module()} function they will only be reloaded if the
699
\code{PythonAutoReload} directive is \code{On}. The default value when
700
the directive is not specified will be \code{On}, so the directive need
701
only be used when wishing to set it to \code{Off} to disable automatic
702
reloading, such as in a production system.
704
Where possible, the \code{PythonAutoReload} directive should only be
705
specified in one place and in the root context for a specific Python
706
interpreter instance. If the \code{PythonAutoReload} directive is used in
707
multiple places with different values, or doesn't cover all directories
708
pertaining to a specific Python interpreter instance, then problems can
709
result. This is because requests against some URLs may result in modules
710
being reloaded whereas others may not, even when through each URL the
711
same module may be imported from a common location.
713
If absolute certainty is required that module reloading is disabled and
714
that it isn't being enabled through some subset of URLs, the
715
\code{PythonImport} directive should be used to import a special module
716
whenever an Apache child process is being created. This module should
717
include a call to the \code{apache.freeze_modules()} function. This
718
will have the effect of permanently disabling module reloading for the
719
complete life of that Apache child process, irrespective of what value
720
the \code{PythonAutoReload} directive is set to.
722
Using the new ability within mod_python 3.3 to have \code{PythonImport}
723
call a specific function within a module after it has been imported,
724
one could actually dispense with creating a module and instead call
725
the function directory out of the \code{mod_python.apache} module.
729
PythonImport mod_python.apache::freeze_modules interpreter_name
732
Where module reloading is being undertaken, unlike the core module
733
importer in versions of mod_python prior to 3.3, they are not reloaded on
734
top of existing modules, but into a completely new module instance. This
735
means that any code that previously relied on state information or data
736
caches to be preserved across reloads will no longer work.
738
If it is necessary to transfer such information from an old module to the
739
new module, it is necessary to provide a hook function within modules to
740
transfer across the data that must be preserved. The name of this hook
741
function is \code{__mp_clone__()}. The argument given to the hook
742
function will be an empty module into which the new module will subsequently
745
When called, the hook function should copy any data from the old module
746
to the new module. In doing this, the code performing the copying should
747
be cognizant of the fact that within a multithreaded Apache MPM that
748
other request handlers could still be trying to access and update the
749
data to be copied. As such, the hook function should ensure that it uses
750
any thread locking mechanisms within the module as appropriate when
751
copying the data. Further, it should copy the actual data locks
752
themselves across to the new module to ensure a clean transition.
754
Because copying integral values will result in the data then being
755
separate, it may be necessary to always store data within a dictionary so
756
as to provide a level of indirection which will allow the data to be
757
usable from both module instances while they still exist.
762
import threading, time
764
if not globals().has_key('_lock'):
765
# Initial import of this module.
766
_lock = threading.Lock()
767
_data1 = { 'value1' : 0, 'value2': 0 }
770
def __mp_clone__(module):
773
module._data1 = _data1
774
module._data2 = _data2
778
Because the old module is about to be discarded, the data which is
779
transferred should not consist of data objects which are dependent on
780
code within the old module. Data being copied across to the new module
781
should consist of standard Python data types, or be instances of classes
782
contained within modules which themselves are not candidates for
783
reloading. Otherwise, data should be migrated by transforming it into
784
some neutral intermediate state, with the new module transforming it back
785
when its code executes at the time of being imported.
787
If these guidelines aren't heeded and data is dependent on code objects
788
within the old module, it will prevent those code objects from being
789
unloaded and if this continues across multiple reloads, then process size
790
may increase over time due to old code objects being retained.
792
In any case, if for some reason the hook function fails and an exception
793
is raised then both the old and new modules will be discarded. As a last
794
opportunity to release any resources when this occurs, an extra hook
795
function called \code{__mp_purge__()} can be supplied. This function will
796
be called with no arguments.
369
800
\begin{funcdesc}{allow_methods}{\optional{*args}}