[mod_python] The new module loader

Dan Eloff dan.eloff at gmail.com
Thu Apr 20 22:29:41 EDT 2006


Neat, it works like you said it would, very nice. I'm not sure how you
managed to reload parent when child changes, I'd like to take a peek at that
later, good job.

__purge__ is called if __clone__ raises and exception, to notify the
original module that it wasn't able to be cloned, and now it's just going to
be dumped without the data being copied over.

I think I figured out what you used the intialized flag for in child.py, it
let's you avoid initializing the parts that get overwritten by __clone__()
in the event that __clone__ is called, afterall, it could be an expensive
operation. I don't understand why you keep a reference in a temporary
location and then lock the object and copy outside of __clone__, why not
lock and copy inside __clone__?

Thanks,
-Dan

On 4/20/06, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
>
> Dan Eloff wrote ..
> > Yay! I've got it now, and it compiled fine. I suspect something that
> will
> > still be a hitch is the need for that __clone__() method you talked
> about.
> > How do I use it, is it documented anywhere, or is there an example
> > __clone__() method I can see?
> >
> > Thanks,
> > -Dan
>
> Note that the __clone__() method is something that I added because of
> a need I saw. I doubt that anyone else has burrowed into the code for
> the importer deep enough yet to understand what it is necessarily all
> about and thus the whole concept hasn't been peer reviewed as such.
> Thus whether it is included at all, or in the form as it currently exists
> is not final.
>
> That said, I have attached a very contrived set of examples of migrating
> data from an old module to a new. It looks messy purely because of
> the different examples and support for multithreading. If a module is
> complicated and you want to be able to support multithreading and
> module reloading, it will be complicated. I've seen a bit of dubious code
> posted in the past which given the right conditions would blow up,
> but without a proper solution have generally refrained from pointing
> out the problems.
>
> Put the attached file in a directory configured for mod_python.publisher
> with a configuration something like:
>
>   PythonInterpreter testing
>   AddHandler mod_python .py
>   PythonHandler mod_python.publisher
>   PythonDebug On
>   PythonAutoReload On
>
> The "testing" interpreter is the one I have the new module importer
> enabled for. Ie., in main Apache configuration, outside of all container
> directives have:
>
>   PythonOption mod_python.future.importer testing
>
> Check that all works by accessing page as "child.py". Once it works,
> copy "child.py" to "parent.py" and add at the start the line:
>
>   import child
>
> Then access "parent.py".
>
> Now for some fun, update modification time on "parent.py" by touching
> it or resaving it. Watch how reload counter increments. Touch it again
> and see how transfer counter increments. Work out from code what
> each of the bits is doing and why certain things are done certain ways.
> If you don't understand, ask.
>
> Now for some tricky stuff. Touch the "child.py" file, but still access
> "parent.py". See how the parent module gets reloaded. Keep touching
> "child.py" if you don't believe what is happening. Also access "child.py"
> from browser as well to see how was also reloaded.
>
> What is happening here is that new module importer doesn't just look
> at page you are accessing, but looks at modules it imports and if the
> child changes, the parent gets reloaded automatically along with the
> child. With this working, you avoid all the horrible problems that exist
> now of having to force reloads by touching all files or restarting Apache.
>
> Finally, note how the reload is occurring even though child was imported
> using "import" statement. This is because underneath it is magically
> mapping to apache.import_module(). Change the "import" from:
>
>   import child
>
> to:
>
>   child = apache.import_module("child")
>
> and you will see it works the same.
>
> The rules are as per that incomplete document I referred you to
> about the module importer.
>
> BTW, you cant totally avoid Apache restarts when you start using
> __clone__() unless you are quite careful that it handles gracefully
> not finding the data in an existing module that it is looking to copy
> across. Anyway, experiment and you will see what I mean.
>
> You might also see if you can work out what __purge__() function
> does by looking at source code. :-)
>
> Hope this provides you with hours of fun.
>
> Graham
>
> > On 4/20/06, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
> > >
> > > Quoting from some of Jim's notes:
> > >    To access the Subversion repositories anonymously, you will need a
> > > Subversion client.
> > >