[Twisted-Python] Automatically refresh imported module from source
Hello All, My name is Thaddeus, and I am new to both Python and Twisted (I found out about the language and platform yesterday). I am doing some experimentation to see how Twisted would benefit our organization. So far Python blows every other language I've seen out of the water, in terms of efficiency and low cost of change. What I'd like to know is if there is a way to modify the way twisted web handles requests from within RPY scripts to external py modules. If I modify the py module while twisted web is running, I would like to see subsequent Import requests to that module reference the newly saved version. I realize that TwistedWeb must be caching the imported modules so future requests don't require it to re-read from file, but it would seem that it should at least check the modules modified date to make sure it still has a current copy whenever the module is requested. Here's the source for an example that demonstrates my point: #Begin Test.rpy from twisted.web import resource import TestModule class MyResource(resource.Resource): def render(self, request): return TestModule.SayHello() resource = MyResource() #End Test.rpy #Begin TestModule.py def SayHello(): return "Hello World!!!" #End Testmodule.py If I point my browser to test.rpy, "Hello World!!!" is displayed. This is great so far. But when I open up pythonwin, and modify TestModule.py to the following: #Begin TestModule.py def SayHello(): return "Wazzaaap World??!!!" #End Testmodule.py And I hit refresh on my browser, "Hello World!!!" still displays. It is not until I stop and restart the web server that the change is reflected, and I am greeted with the revised verbeage. Must I bring the web server down, every time a make a simple change to an external module? There's gotta be a way around this. The main reason I'm looking into this platform is to decrease cost of change. Please let me know, Thaddeus Jacobs IT Developer Kinematic Automation, Inc. mailto:tjacobs@kinematic.com
On Fri, Feb 28, 2003 at 04:02:19PM -0800, Thad Jacobs wrote:
I realize that TwistedWeb must be caching the imported modules so future requests don't require it to re-read from file, but it would seem that it should at least check the modules modified date to make sure it still has a current copy whenever the module is requested.
Nah, twisted.web doesn't do that, Python does. Python never re-executes a module unless it's explicitly told to; the cache is in `sys.modules'. And also, it would not be good if twisted.web checked to reload the modules every time - that would hurt performance when it's not really needed - you only really need it during development.
Here's the source for an example that demonstrates my point:
#Begin Test.rpy
from twisted.web import resource import TestModule
Right here, just insert `reload(TestModule)' -- when you're in active development. Just take it out after you're ready to deploy. One caveat is if you have persistent objects that aren't re-created from the .rpy on every request, `reload' won't update them. If that is your case (which it isn't in your example), you should use twisted.python.rebuild. from twisted.python.rebuild rebuild.rebuild(TestModule) rather rather reload(TestModule).
class MyResource(resource.Resource): def render(self, request): return TestModule.SayHello()
resource = MyResource()
#End Test.rpy
#Begin TestModule.py
def SayHello(): return "Hello World!!!"
#End Testmodule.py
HTH. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
participants (2)
-
Christopher Armstrong -
Thad Jacobs