简体   繁体   English

mysql数据库中的并发访问问题

[英]Concurrent access problem in mysql database

Hi i'm coding a python script that will create a number child processes that fetch and execute tasks from the database. 嗨,我正在编码一个python脚本,它将创建一些子进程,这些子进程从数据库中获取并执行任务。

The tasks are inserted on the database by a php website running on the same machine. 任务是通过在同一台计算机上运行的php网站插入数据库中的。

What's the good (need this to be fast) way to select and update those tasks as "in progress" to avoid to be selected by multiple times by the python scripts 有什么好办法(需要快速)选择和更新“正在进行中”的任务,从而避免被python脚本多次选择

edit: database is mysql 编辑:数据库是mysql

Thanks in advance 提前致谢

Without knowing more about your architecture, I suggest the following method. 在不了解您的体系结构的情况下,我建议使用以下方法。

1) Lock Process table
2) Select ... from Process table where State="New"
3) processlist = [list of process id''s from step 2]
4) Update Process table set State="In progress" where ProcessId in [processlist]
5) Unlock Process table.

A way to speed things up is to put the process into a stored procedure, and return the selected row from that procedure. 一种加快处理速度的方法是将流程放入存储过程,然后从该过程返回所选的行。 That way, only one trip to the db server. 这样,仅需一次访问数据库服务器。

Use an InnoDB table Tasks , then: 使用InnoDB表Tasks ,然后:

select TaskId, ... from Tasks where State="New" limit 1;
update Tasks set State="In Progress" where TaskId=<from above> and State="New";

if the update succeeds, you can work on the task. 如果更新成功,则可以执行任务。 Otherwise, try again. 否则,请重试。

You'll want an index on TaskId and State. 您需要在TaskId和State上建立索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM