If you use RDBMS, you can use its "LOCK" mechanism.
For example, while one "SELECT FOR UPDATE" transaction locks a row, the other "SELECT FOR UPDATE" transactions with the row must wait.
# You can use any Python DB API.
[SQL] BEGIN;
[SQL] SELECT col_name FROM table_name where id = 1 FOR UPDATE;
[Process some python code]
[SQL] COMMIT;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…