From a37885041b442b381ae58a9322b8bc8256992b7a Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Mon, 16 Sep 2019 13:30:45 +0100 Subject: Add a lock to the task assignment While it shouldn't take very long, this should nonetheless avoid race conditions on the task assignment logic. Following the discussion here: https://stackoverflow.com/q/18213619 --- app/utils/tasks.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'app/utils') diff --git a/app/utils/tasks.py b/app/utils/tasks.py index b0f5c27..8356876 100644 --- a/app/utils/tasks.py +++ b/app/utils/tasks.py @@ -5,13 +5,26 @@ """ import random +import multiprocessing from flask import current_app from app.models import Dataset, Task +TASK_LOCK = multiprocessing.Lock() + def generate_user_task(user): + task = None + TASK_LOCK.acquire(timeout=10) + try: + task = realgenerate_user_task(user) + finally: + TASK_LOCK.release() + return task + + +def realgenerate_user_task(user): """ Generate new task for a given user. -- cgit v1.2.3