Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy/MySQL在序列化访问上的死锁_Python_Mysql_Sqlalchemy_Deadlock - Fatal编程技术网

Python SQLAlchemy/MySQL在序列化访问上的死锁

Python SQLAlchemy/MySQL在序列化访问上的死锁,python,mysql,sqlalchemy,deadlock,Python,Mysql,Sqlalchemy,Deadlock,我有一个与sqlalchemy一起使用的InnoDB表死锁的大问题 sqlalchemy.exc.InternalError:(mysql.connector.errors.InternalError)1213(40001):尝试获取锁时发现死锁;尝试重新启动事务 我已经序列化了访问,但仍然出现死锁错误 此代码在每个函数的第一次调用时执行。每个线程和进程都应该在这里等待,直到它获得锁为止。它被简化了,因为选择器被移除了 # The work with the index -1 alway

我有一个与sqlalchemy一起使用的InnoDB表死锁的大问题

sqlalchemy.exc.InternalError:(mysql.connector.errors.InternalError)1213(40001):尝试获取锁时发现死锁;尝试重新启动事务

我已经序列化了访问,但仍然出现死锁错误

此代码在每个函数的第一次调用时执行。每个线程和进程都应该在这里等待,直到它获得锁为止。它被简化了,因为选择器被移除了

    # The work with the index -1 always exists.
    f = s.query(WorkerInProgress).with_for_update().filter(
        WorkerInProgress.offset == -1).first()
我已将代码缩减到最小状态。我目前只对下一个\u切片的方法运行并发调用。会话处理、回滚和死锁处理在外部处理

即使所有访问都序列化了,我也会遇到死锁。我也尝试在offset=-1实体中增加一个重试计数器

def next_slice(self, s, processgroup_id, itemcount):
    f = s.query(WorkerInProgress).with_for_update().filter(
        WorkerInProgress.offset == -1).first()

    #Take first matching object if available / Maybe some workers failed
    item = s.query(WorkerInProgress).with_for_update().filter(
        WorkerInProgress.processgroup_id != processgroup_id,
        WorkerInProgress.processgroup_id != 'finished',
        WorkerInProgress.processgroup_id != 'finished!locked',
        WorkerInProgress.offset != -1
        ).order_by(WorkerInProgress.offset.asc()).limit(1).first()

    # *****
    # Some code is missing here. as it's not executed in my testcase

    # Fetch the latest item and add a new one 
    item = s.query(WorkerInProgress).with_for_update().order_by(
        WorkerInProgress.offset.desc()).limit(1).first()     

    new = WorkerInProgress()
    new.offset = item.offset + item.count
    new.count = itemcount
    new.maxtries = 3
    new.processgroup_id = processgroup_id
    s.add(new)
    s.commit()
    return new.offset, new.count
我不明白为什么会出现僵局


通过在一个查询中获取所有项,我减少了死锁,但仍然得到了死锁。也许有人能帮我。

我终于解决了我的问题。这些都在文档中,但我必须先理解它

如果由于以下原因导致交易失败,请随时准备重新发布交易 僵局死锁并不危险。再试一次

资料来源:

通过更改此部件的体系结构,我已经解决了我的问题。我仍然会遇到很多死锁,但它们几乎出现在短期运行的方法中。 我已将工作表拆分为锁定部分和非锁定部分。锁定部分上的操作现在非常短,在get_slice、finish_slice和fail_slice操作期间没有数据处理

具有数据处理功能的事务部分现在处于非锁定部分,并且没有对表行的并发访问。结果存储在锁定表的finish_slice和fail_slice中

最后,我也找到了关于stackoverflow的一个很好的描述。在识别正确的搜索词之后。