Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock a critical section in Django?

Tags:

python

django

I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across processes, or barring that a way to stop a Django server from forking.

like image 787
stinkypyper Avatar asked Jul 14 '09 02:07

stinkypyper


People also ask

What does lock () do in Python?

Using Lock to prevent the race condition To prevent race conditions, you can use the Lock class from the threading module. A lock has two states: locked and unlocked. By default, the lock has the unlocked status until you acquire it.

What is critical section in Python?

A "critical section" is a chunk of code in which, for correctness, it is necessary to ensure that only one thread of control can be in that section at a time. In general, you need a critical section to contain references that write values into memory that can be shared among more than one concurrent process.

What is mutual exclusion in Python?

Mutex means Mutual Exclusion. It means that at a given specific time, only one thread can use a particular resource. If one program has multi-threads, then mutual exclusion restricts the threads to use that particular resource simultaneously.

How do I switch between local and production settings in Django?

By default, new Django projects created with the startproject command have local development settings. The developer is responsible for updating them to production settings. These days, the standard way to switch between local and production settings is with environment variables.

What are Django’s default development settings?

Like most web frameworks, Django defaults to local development settings when a new project is created. The onus is on the developer to effectively manage both local defaults and customized production settings.

How to secure the Django admin app?

The Django admin app is a powerful built-in feature. However, since every Django project has it located at /admin by default, it is easy for a hacker to try to force their way into any Django website at this URL. An easy way to secure your admin is to simply change the URL for the admin.

How to solve the critical section in a thread?

A simple solution to the critical section can be thought of as shown below, A thread must acquire a lock prior to executing a critical section. The lock can be acquired by only one thread. There are various ways to implement locks in the above pseudo-code. Let us discuss them in future articles.


4 Answers

If you use RDBMS, you can use its "LOCK" mechanism. For example, while one "SELECT FOR UPDATE" transaction locks a row, the other "SELECT FOR UPDATE" transactions with the row must wait.

# You can use any Python DB API.
[SQL] BEGIN;

[SQL] SELECT col_name FROM table_name where id = 1 FOR UPDATE;

[Process some python code]

[SQL] COMMIT;
like image 113
takaomag Avatar answered Oct 23 '22 07:10

takaomag


Use the Django builtin select_for_update function.
https://docs.djangoproject.com/en/1.8/ref/models/querysets/#select-for-update
From the docs:
Returns a queryset that will lock rows until the end of the transaction, generating a SELECT ... FOR UPDATE SQL statement on supported databases.

For example:

entries = Entry.objects.select_for_update().filter(author=request.user)

All matched entries will be locked until the end of the transaction block, meaning that other transactions will be prevented from changing or acquiring locks on them.

like image 43
Tal Weiss Avatar answered Oct 23 '22 06:10

Tal Weiss


You need a distributed lock manager at the point where your app suddenly needs to run on more than one service. I wrote elock for this purpose. There are bigger ones and others have chosen to ignore every suggestion and done the same with memcached.

Please don't use memcached for anything more than light advisory locking. It is designed to forget stuff.

I like to pretend like filesystems don't exist when I'm making web apps. Makes scale better.

like image 22
Dustin Avatar answered Oct 23 '22 06:10

Dustin


You could use simple file locking as a mutual exclusion mechanism, see my recipe here. It won't suit all scenarios, but then you haven't said much about why you want to use this type of locking.

like image 5
Vinay Sajip Avatar answered Oct 23 '22 08:10

Vinay Sajip