0

I have a function in sqlalchemy (python3) that I first select from a table and then update those selected rows. I want to lock these two executions so that if my function is triggered multiple times at the same time, the execution should be in a queue and not concurrently becasue this will produce unexpected results. I am having trouble understanding how to implement this. Any help is appreciated.

def myfunction():
      qry = db.session.query(MyTable).filter(MyTable.is_set==False).limit(4).all()
      for row in qry:
         row.is_set = True
      db.session.commit()

This function might be called at the same time, and if I don't use a lock mechanism it would generate unexpected results.

So I want to make sure execution of this function would be one after the other instead of being concurrently

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Mia
  • 39
  • 5
  • I'd recommend adding some code examples to your question of what you tried and what the outcome was. – Kerwin Sneijders Sep 06 '20 at 23:45
  • I added an example. So in this example if the function runs two times, it might be in the middle of the first one (for example the select has been done and two rows have been updated) that the second one runs. In this case I don't want the second execution to select two rows that have is_set == False but actually they are about to set to true. – Mia Sep 06 '20 at 23:53

0 Answers0