Jim Fulton wrote:
Chris Spencer wrote:

When a non-ghost object is invalidated, it's _p_invalidate method is
called.  You could conceivably override this method, for example to
reload state and update a display.

Could this be used in the chatter.py demo to directly notify the client of new messages instead of forcing the client to continually poll the server for changes?

For instance, using chatter.py as a guide, I wrote a simple script (shown below) that modifies a persistent object on a zeoserver, and recieves notifications of changes to the object, although the local callback is never called.

If you run two instances of the script, you should see one recieve a couple notifications and then crash, complaining of a TransactionFailedError, which itself complains of a previously failed ConflictError. I'm doing a conflict check, just like in chatter.py, so what would be causing this?

Regards,
Chris

#!/usr/bin/python
'''
Modifies an object on a ZEO server, and hopefully gets notified of modifications
made by others.
'''

import time
from random import randint

import transaction
from persistent import Persistent

from ZEO import ClientStorage
import ZODB
from ZODB import DB
from ZODB.POSException import ConflictError

class Node(Persistent):

    def __init__(self):
        self.value = 0
        self._v_update_cb = None

    def set(self, value):
        while 1:
            try:
                # change
                self.value = value
                self._p_changed = 1
                transaction.commit()
            except ConflictError:
                # try again later
                time.sleep(.2)
                pass
            else:
                 # commited
                break

    def _p_invalidate(self):
        Persistent._p_invalidate(self)
        print 'invalidated'
        if hasattr(self, '_v_update_cb') and callable(self._v_update_cb):
            self._v_update_cb()

def notify():
    print 'notified of new value!'

# Change next line to connect to your ZEO server
addr = 'localhost', 9090
storage = ClientStorage.ClientStorage(addr)
db = DB(storage)
conn = db.open()
root = conn.root()

# initialize object
obj = root.get('testobj')
if obj is None:
    root['testobj'] = Node()
    transaction.commit()
    obj = root.get('testobj')
obj._v_update_cb = notify

# constantly modify object
while 1:
    obj.set(obj.value+1)
    print 'obj =',obj.value
    time.sleep(randint(1,3))

_______________________________________________
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