1👍
You can cheat a bit by storing the result manually in the database. Let me explain how this will help.
For example, if using RDBMS (table with columns – task_id, state, result):
View part:
- Use transaction management.
- Use SELECT FOR UPDATE to get row where task_id == “long-task-%d” % user_id. SELECT FOR UPDATE will block other requests until this one COMMITs or ROLLBACKs.
- If it doesn’t exist – set state to PENDING and start the ‘some_long_task’, end the request.
- If the state is PENDING – inform the user.
- If the state is SUCCESS – set state to PENDING, start the task, return the file pointed to by ‘result’ column. I base this on the assumption, that you want to re-run the task on getting the result. COMMIT
- If the state is ERROR – set state to PENDING, start the task, inform the user. COMMIT
Task part:
- Prepare the file, wrap in try, catch block.
- On success – UPDATE the proper row with state = SUCCESS, result.
- On failure – UPDATE the proper row with state = ERROR.
5👍
I solved this with Redis. Just set a key in redis for each task and then remove the key from redis in task’s after_return method. Redis is lightweight and fast.
- [Django]-Default value for user ForeignKey with Django admin
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-Django – getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
5👍
I don’t think (as Tomek and other have suggested) that using the database is the way to do this locking. django has built-in cache framework, which should be sufficient to accomplish this locking, and must faster. See:
http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html#cookbook-task-serial
Django can be configured to use memcached
as its cache backend, and this can be distributed across multiple machines … this seems better to me. Thoughts?
- [Django]-Django F() division – How to avoid rounding off
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-How to save pillow image object to Django ImageField?