ZODB broken instance

91 Views Asked by At

I am trying to persist an object reference using only ZODB in a FileStorage database. I made a test to analyze its performance, but the object when it is loaded it appears to be broken.

The test consists on:

  1. create an object in one script and write it to database.
  2. In another script read that object from the same database and use it there.

zodb1.py image from CMD

zodb2.py image from CMD

zodb1.py

import ZODB
from ZODB.FileStorage import FileStorage
import persistent
import transaction

storage = FileStorage('ODB.fs')
db = ZODB.DB(storage)
connection = db.open()
ODB = connection.root()

print(ODB)

class Instrument(persistent.Persistent):

    def __init__(self, name, address):
        self.name = name
        self.address = address
    
    def __str__(self):
        return f'Instrument - {self.name}, ID: {self.address}'

camera = Instrument(name='Logitech', address='CAM0')


ODB['camera'] = camera
ODB._p_changed = True
transaction.commit()

print(ODB)

ob = ODB['camera']
print(ob)
print(dir(ob))

zodb2.py

import ZODB, ZODB.FileStorage
import persistent
import transaction

connection = ZODB.connection('ODB.fs')

ODB = connection.root()

print(ODB)

ob = ODB['camera']
print(ob)
print(dir(ob))

Am I missing something important? I've read the ZODB's documentation and I see no other configuration process or another way to aproach this.

Thank you in advance.

1

There are 1 best solutions below

0
Godefroid Chapelle On

I think that the problem you see is because zodb2.py has no knowledge of the Instrument class defined in zodb1.py.

I guess that if you moved your class to a separate module and imported it in both zodb1 and zodb2, you would not see a broken object.