Lock in sqlalchemy

I am confused about how to change a table from several different processes at the same time. I tried using Query.with_lockmode(), but it doesn't seem to do what I expect from it, which will prevent two processes from simultaneously requesting the same lines. Here is what I tried:

import time
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *

engine = create_engine('mysql://...?charset=utf8&use_unicode=0', pool_recycle=3600, echo=False)
Base = declarative_base(bind=engine)
session = scoped_session(sessionmaker(engine))

class Test(Base):
    __tablename__ = "TESTXYZ"
    id = Column(Integer, primary_key=True)
    x = Column(Integer)

def keepUpdating():
    test = session.query(Test).filter(Test.id==1).with_lockmode("update").one()

    for counter in range(5):
        test.x += 10
        print test.x
        time.sleep(2)

    session.commit()


keepUpdating()

If I run this script twice twice, I get session.query(Test).filter(Test.id==1).one().xequal to 50, not 100 (assuming there were 0 to start with), which I was hoping for. How do I get both processes to update values ​​at the same time or wait for the second until the first is done?

+3
source share
1 answer

MyISAM? InnoDB, ( ) MyISAM.

+4

Source: https://habr.com/ru/post/1717444/


All Articles