dp926460 2012-06-09 16:50
浏览 8
已采纳

由于延迟时间和多个请求,避免双DB查询

I have a MySql database linked to my website, that has a table with a task list (columns are separated by "/" below), i.e.:

task1 / doTask1.sh / todo

task2 / doTask2.sh / done

task3 / doTask3.sh / todo

task4 / doTask4.sh / todo

etc...

On every request to my website, I want the "todo" tasks to be executed and then marked as "done" by the server. The task can only be executed once. But here comes the problem.

What if:

User A selects the tasks that are marked as "todo". doTask1.sh executes and is marked as done, triggered by request from user A. User B selects the tasks that are marked as "todo" (task3 and task3 only, since task1 is done). doTask3.sh executes and is marked as done, triggered by request from user A. doTask3.sh executes again, triggered by request from user B. Same for doTask4.sh.

Can this happen using PHP/Nginx/MySql, due to simultaneous DB connections? If yes, how can this problem be bypassed/avoided?


In this example, I used doTaskx.sh to simplify the situation. In reality, this column would contain a description of the task and the PHP script powering my website would execute a task depending on that description.

  • 写回答

2条回答 默认 最新

  • duanbo5230 2012-06-09 17:04
    关注

    Yes, this can happen.

    One approach for solving this problem is to do the following:

    • Create a "pending" status".
    • When a request comes in to execute a task, run an update on the database setting everything matching the ID and status = "todo" to "pending".
    • If the number of records updated is 1, execute the task and mark it as "done".
    • If the number of records updated is 0, it means another request got there first.
    • Either way, update the user interface to show that the task is pending/done.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么