I'm trying to make ZODB dynamically reload classes when their source
code changes. However, even though I close my connection, reload the
module, then call Connection.resetCaches(), my object still doesn't
use the new class definition. Is there any way to fix this?

Attached is my simple test script and a sample module containing the
class Foo to be updated. To see the problem, modify Foo's return value
in sample.py while simultaneously running test.py.

Chris
'''
Test dynamically loading class updates with ZODB.
'''

import os
import time

from ZODB import FileStorage, DB, Connection
import transaction

import sample

# open db
storage = FileStorage.FileStorage('test.db')
db = DB(storage)
conn = db.open()
try:
    try:
        lastUpdated = time.time()
        while 1:
            if lastUpdated < os.stat(sample.__file__)[8]:
                # code changed so update objects
                print 'loading update'
                transaction.commit()
                conn.close()
                sample = reload(sample)
                Connection.resetCaches()
                conn = db.open()
                lastUpdated = time.time()
                print 'update loaded'
            else:
                # no update, so just call foo
                root = conn.root()
                root.setdefault('foo', sample.Foo())
                print 'foo() => %s' % root['foo']()
            time.sleep(1)
    except Exception, e:
        print 'error: %s' % e
finally:
    print 'closing system'
    transaction.commit()
    conn.close()
    db.close()
    storage.close()
    print 'system closed'
from persistent import Persistent

class Foo(Persistent):
    def __call__(self):
        return 1
_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to