Jython and super_reload?

Ype Kingma ykingma at accessforall.nl
Sat Aug 21 13:37:12 EDT 2004


Dave Benjamin wrote:

> In article <412458a6$0$45379$e4fe514c at dreader19.news.xs4all.nl>, Ype
Kingma wrote:
>> 
>> This works for classes: 
>> 
>> http://www.jython.org/docs/jreload.html
>> 
>> You can unload a module in Jython by removing it from sys.modules,
>> and (off course) by removing all other references to it.
>> 
>> To be sure about what happens you can tell the JVM to be verbose
>> about garbage collection of Java classes, and explicitly call
>> java.lang.System.gc() in some test scripts.
> 
> Thanks for the link and the comments, but I don't think this is quite what
> I'm trying to do here. I'm not trying to reload Java classes, I'm trying
to
> reload Python modules such that any instances of the old version of that
> module's classes get updated to use the new methods. For instance:
> 
> mymod.py
> --------
> class A:
>     def test(self):
>         print 'hello'
> 
> app
> ---
> import mymod
> a = A()
> a.test()
> (output: 'hello')
> 
> mymod.py
> --------
> class A:
>     def test(self):
>         print 'goodbye'
> 
> app
> ---
> reload(mymod)
> a.test()
> (output: still 'hello')
> 
> # This is what I've been unable to accomplish:
> from magic import super_reload
> super_reload(mymod)
> a.test()
> (output: 'goodbye')
> 
> Does that make sense? The end result is that I want to be able to modify
> classes in a running application and have this affect all existing class
> instances. This is important because I am working in an application server

I don't think the Python language supports it.
One could replace the module in sys.modules, but that does not remove the
reference to the old module and its contents in existing instances.

> environment where restarts are costly and time-consuming, and instances
are
> often kept in user sessions. Currently, any changes to a class require
that
> the session be reinitialized (ie. you have to log out and log back in),
> which makes incremental testing and development awkward. 

You might solve the problem by convincing the users to reinstantiate their
classes after replacing the module in sys.modules.

Replacing the Python class of a Python instance is tricky (to say the least)
because of Python's dynamic nature. And even without Python dynamics one
can get into strange situations.

When you convince the users to reinstantiate their classes,
my guess is that sooner rather than later some of your users will manage
to bring their sessions into an irreproducable (error) state because they
forgot to replace some instances.

In a Java JVM one can have multiple fully independent Jython
system states by using different Java class loaders. This is safe, but it
costs some memory. This has been done by others, please check the
jython-user and jython-dev archives.

Good luck,
Ype




More information about the Python-list mailing list