>I think the error throw of the IntegrityError is totally expensive
>from both a DB perspective as well as a python perspective.  if
>missing data is truly so rare then it might be fine, but the
>necessesity of then using SAVEPOINT seems to complicate things more
>than necessary.  but you'd have to benchmark different scenarios to
>see which is best.

Thanks, but unfortunately, cannot get rid of tracebacks.


"""
from threading import Thread, Lock
from sqlalchemy import create_engine, MetaData, Table, Column, types,
schema, insert, update, delete
from sqlalchemy.orm import mapper, backref, scoped_session,
sessionmaker
from sqlalchemy.exceptions import IntegrityError

metadata = MetaData()
u_table = Table('auth_user', metadata, \
    Column('id', types.Integer, primary_key=True), \
    Column('username', types.String(30)))
f_table = Table('fs_file', metadata, \
    Column('id', types.Integer, primary_key=True), \
    Column('user_id', None, schema.ForeignKey("auth_user.id")), \
    Column('path', types.String, unique=True), \
    Column('ls', types.PickleType, nullable=False))

class User(object):
    pass

mapper(User, u_table)
mapper(Path, f_table)

session = scoped_session(sessionmaker(transactional=True,
autoflush=False))
db = create_engine("postgres://...", strategy="threadlocal")
session.configure(bind=db)
db.echo = True


class LockingManager(object):
    def __init__(self):
        self._lock = Lock()
        if not metadata.is_bound():
            metadata.bind = db

    def delete(self, uid=None):
        self._lock.acquire()
        try:
            session.execute(delete(f_table), {'user_id': uid})
            session.commit() #line 88
        finally:
            self._lock.release()

    def insert(self, uid=None, path=None, ls=None):
        ...
    def update(self, uid=None, path=None, ls=None):
        ...

    def select(self, username=None):
            stuff = session.query(User).filter_by(username=username).first()
        return stuff


def arrange(Thread):
    def __init__ (self, event, manager):
        Thread.__init__(self)
        self.e = event
        self.manager = manager
    def run(self):
        theone = self.manager.select(username='me')
        self.manager.delete(uid=theone.id)
        ...

class Watch(ProcessEvent):
    def __init__(self, watch_manager, lock_manager):
        self._watch_manager = watch_manager
        self._lock_manager = lock_manager

    def process_IN_DELETE(self, event):
        super(Watch, self).process_default(event)
        arrange(event, self._lock_manager).start()

def main():
    lm = LockingManager()
    wm = WatchManager()
    wm.add_watch(HOMEDIR, mask, proc_fun=Watch(wm, lm), rec=True,
auto_add=True)
    ...

if (__name__ == "__main__"):
    main()
"""

Traceback is happening on delete and always look like this:

"""
Traceback (most recent call last):
  File "threading.py", line 442, in __bootstrap
    self.run()
  File "./camper.py", line 189, in run
    walk(self.manager, theone, root)
  File "./camper.py", line 101, in walk
    manager.delete(uid=theone.id)
  File "./camper.py", line 88, in delete
    session.commit()
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/scoping.py",
line 74, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/session.py",
line 484, in commit
    self.transaction = self.transaction.commit()
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/session.py",
line 211, in commit
    self.session.flush()
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/session.py",
line 686, in flush
    self.uow.flush(self, objects)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 207, in flush
    flush_context.execute()
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 434, in execute
    UOWExecutor().execute(self, head)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 1053, in execute
    self.execute_save_steps(trans, task)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 1067, in execute_save_steps
    self.save_objects(trans, task)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 1058, in save_objects
    task.mapper.save_obj(task.polymorphic_tosave_objects, trans)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py",
line 1089, in save_obj
    raise exceptions.ConcurrentModificationError("Updated rowcount %d
does not match number of objects updated %d" % (rows, len(update)))
ConcurrentModificationError: Updated rowcount 0 does not match number
of objects updated 1
"""

As far as I understand, one query at a time executing at a time now.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to