I’m building an XML-RPC server using Twisted which periodically checks if the timestamps of its source files have changed and reloads them using rebuild.
from twisted.python.rebuild import rebuild
rebuild(mymodule)
The functions the server exposes get reloaded fine, but there in another protocol class active which calls callback functions on the same class of mymodule but they don’t use the reloaded version of the functions. This protocol simply has a dict with normal functions as values.
I found this mixin class which is intended to deal with limitations of rebuild.
http://twistedmatrix.com/documents/current/api/twisted.python.rebuild.Sensitive.html
How do I make sure my callbacks use up to date code?
You’re correct; this is a limitation of
twisted.python.rebuild. It’s on updating the__class__attribute of existing instances, and imported functions and classes.One way to deal with this would be to simply submit a patch to Twisted that fixes
rebuildfor this case.However, if you want to use
Sensitivefor its intended purpose, you can also implement your callback-holding class to work withrebuildon current versions of Twisted. The following examples demonstrates how to use all three methods of the relevant class to update a dictionary of callbacks pointing at functions. Note that even without theif needRebuildUpdate...test, callingx()directly will always get the most up-to-date version.