[Twisted-Python] Automatically refresh imported module from source

Christopher Armstrong radix at twistedmatrix.com
Fri Feb 28 21:32:47 EST 2003

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

  from twisted.python.rebuild

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


 Twisted | Christopher Armstrong: International Man of Twistery
  Radix  |          Release Manager,  Twisted Project
---------+     http://twistedmatrix.com/users/radix.twistd/

More information about the Twisted-Python mailing list