반응형
mariadb의 select+update 트랜잭션을 원자적으로 실행할 수 없습니다.
mariadb의 "tasks" 테이블을 사용하여 python 스크립트를 병렬화하려고 합니다. 여기서 각 행은 작업을 설명하고 실행 중인 각 스크립트 인스턴스는 다음과 같은 단순화된 python + peewee 코드를 수행합니다.
with db.transaction() as txn:
result = list(Table.raw("select id from tasks where started=0 limit 1"))
if len(result) == 0:
sys.exit("nothing left to do")
my_next_task = result[0]
# mark this task as started
Table.raw("update tasks set started=1 where id=%s" % my_next_task.id)
# process my_next_task
그러나 스크립트의 두 인스턴스를 동시에 시작하면 두 인스턴스 모두 동일한 작업을 시작합니다.이 경우에 거래가 어떻게 작동하는지 제가 오해하고 있는 것입니까?
이거 먹어봐요.다음 코드는 작업을 실행하기 전에 하나의 스레드만 잠금을 획득하도록 보장됩니다.
1. 트랜잭션을 수행한다는 사실은 한 번에 하나의 스레드만 업데이트할 수 있는지 확인합니다.
2.시작 = 0을 확인해야 합니다.이렇게 하면 하나의 스레드만 업데이트할 수 있습니다.
with db.transaction() as txn:
result = list(Table.raw("select id from tasks where started=0 limit 1"))
// The update query should return number of rows updated. So if a thread updates the task, it should return 1, which means it acquired a lock. If not, it means the thread didn't acquire the lock
got_lock = Table.raw("update tasks set started=1 where id=%s and started = 0" % my_next_task.id) > 0
if len(result) == 0:
sys.exit("nothing left to do")
my_next_task = if(got_lock) result[0] else return
언급URL : https://stackoverflow.com/questions/36536725/cant-make-selectupdate-transaction-in-mariadb-execute-atomically
반응형
'sourcecode' 카테고리의 다른 글
RecyclerView 스크롤을 비활성화하는 방법은 무엇입니까? (0) | 2023.07.28 |
---|---|
Ajax HTML 응답에서 본문 태그 찾기 (0) | 2023.07.28 |
사용자 MVCEF Core를 기반으로 다른 데이터베이스에 연결 (0) | 2023.07.28 |
ADODB.Command: 준비된 문이 예상 레코드 수를 반환하지 않습니다. (0) | 2023.07.28 |
Angular App의 구문 오류: 예기치 않은 토큰 < (0) | 2023.07.28 |