Using Django ORM as a Lock for Multiple Processes on Hosts

I am running Django on several websites connected to a shared database server. The database contains a simple table of pending jobs. eg.

class Job(models.Model): name = models.CharField(max_length=255, null=False, help_text='task to do') worker = models.CharField(max_length=255, null=True, help_text='globally unique host name') 

How can I use Django ORM to request a pending job (where the worker is null) and set the name of the worker atom atomically, so that no two Django processes accidentally capture the same job (i.e. avoid the race condition)?

Ideally, I just want to wrap on request / update inside a table lock, but Django ORM doesn't seem to have a built-in function.

+4
source share
1 answer

As already mentioned, celery would be the best tool for this. If you do not want to use Celery:

  • If you are on the latest Django, use select_for_update () to request your job, and then fill in the worker field. What "Pillar" you like in Django ORM. It only locks the affected rows, so it's better than locking the table, allowing more concurrency.

  • If you are in an older version where select_for_update() not available, use the Lock Table (as simple as 10 lines) to protect your critical sections.

+2
source

Source: https://habr.com/ru/post/1413564/


All Articles