Thread synchronization in Django

Is there a way to block a critical area, for example, with Java synchronization in Django?

+4
source share
2 answers

You can use locks to make sure that only one thread will access a specific block of code at a time.

To do this, you simply create a lock object, then acquire a lock in front of the block of code that you want to synchronize. Example:

lock = Lock() lock.acquire() # will block if another thread has lock try: ... use lock finally: lock.release() 

For more information see http://effbot.org/zone/thread-synchronization.htm .

+5
source

Justin's great article, just one thing using python 2.5, makes this way easier

In Python 2.5 and later, you can also use the with statement. When used with a lock, this operator automatically obtains a lock before entering the block and releases it when leaving the block:

from future import with_statement # 2.5 only

with lock: ... access to shared resource

0
source

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


All Articles