Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Re: How to load new class definitions at runtime?

 
Thread Tools Search this Thread
Old 11-11-2004, 09:43 PM   #1
Default Re: How to load new class definitions at runtime?


Carlos Ribeiro wrote:
> I'm looking for ways to load new class definitions at runtime (I'm not

[snip]
> Now, talking about executing arbitrary code retrieved from a database
> isn't exactly a safe proposition. But I can't see any other way to
> make it work. I'm now looking for more ideas on how do other people
> solved the same problem in other projects. Pointers are welcome.


We didn't use a database, but just stored them on disk - we considered the app's
directory to be within our security wall, and made sure it was pretty well
locked down. Basically, if it is compromised, you can already get at all the
secret data, so sneaking in evil Python code doesn't really give you any
advantage. YMMV of course.

As for class definitions, we used the 'new' module to create updated modules
(and also replaced the previous entry in sys.modules - not sure if any other
bases need covering, but it hasn't broken yet ) and then established two
conventions for "hot swappable" modules:

1) No module level code. Modules can have optional Setup() and Teardown()
functions that get called if present (the most typical use case is to pass state
from the old version of the module to the new version - the return value from
Teardown gets passed to the new module's Setup function).

2) Don't store references to hot swap modules for long periods of time. When you
need to use one of thoes modules, you get a reference to a module by calling an
API (e.g. GetModule() ) and it also takes care of updating the module if it has
changed on disk. So we had lots of code like:

def foo(arg):
someLib = GetModule('someLib')
someLib.biff()
...
someLib.bar()

etc. - i.e. the reference is kept for relatively short amounts of time so that
if the module gets reloaded, no code will be using the old version for very long.

Overall it has worked really well; we try to avoid as much as possible modules
that maintain their own state because reloading can be tricky (in our simplistic
implementation you can lose state changes if one occurs between Teardown and
Setup - fortunately for us we haven't had any cases where that matters).

-Dave


Dave Brueck
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
error: java.lang.UnsatisfiedLinkError: Can't load library: /home/nalgo01/libcImpl.so gopinath511 Software 0 01-07-2009 09:13 AM
Eclipse how to add external class with different pkg name jan2321 General Help Related Topics 0 11-06-2008 10:04 AM
70-536, 3 questions blade MCTS 11 03-23-2008 03:47 PM
How to load Two iframe in one aspx page? pratima Software 2 02-02-2008 03:17 AM
Info about load balancing in cisco routers and switches!! vishamr2000 Hardware 0 06-22-2007 05:42 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46