【发布时间】:2016-04-10 23:22:58
【问题描述】:
我正在尝试通过使用 mariadb 中的“任务”表来并行化 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
但是,当我同时启动脚本的 2 个实例时,它们都开始执行相同的任务。我是否误解了在这种情况下交易应该如何工作?
【问题讨论】:
标签: mysql python-3.4 mariadb peewee