Jython and super_reload?

Y

Ype Kingma

Dave said:
There have been several discussions here about how to write a reload()
function that works for classes as well as modules:

http://groups.google.com groups?sourceid=mozclient&ie=utf-8&oe=utf-8&q=super_reload

I have been trying to come up with a solution that works for Jython, and so
far nothing has worked. I was wondering if anyone has attempted or succeeded
in writing such a function for Jython.

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.


Ype
 
D

Dave Benjamin

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
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.

Thanks!
Dave
 
Y

Ype Kingma

Dave said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,058
Members
47,668
Latest member
SamiraShac

Latest Threads

Top