I implemented a network lock using redis, the module had some other
functionalities, but the basic lock class was:

class Lock():
    """
    Regular behavior lock, works across the network, useful for
distributed processes
    """
    def __init__(self, name):
        self.redis = StrictRedis(host='dev3', db=2)
        self.redis._use_lua_lock = False
        self.lock = self.redis.lock(name)

    def __enter__(self, blocking=True):
        # print('Acquiring lock')
        return self.lock.acquire(blocking)

    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
        # print('releasing lock')
        self.lock.release()

    acquire = __enter__
    release = __exit__


On Thu, Dec 18, 2014 at 4:14 PM, Vijay Khemlani <[email protected]> wrote:
>
> I'm not sure that using a cache system as a lock mechanism is a good idea.
>
> What I would do is setup a task queue (using celery and rabbitMQ) with a
> single worker, that way you guarantee that only one task is running at a
> time and you can queue as many as you want. Also, each task has a maximum
> lifespan, after that it kill itself and the workers starts executing the
> next task in the queue automatically.
>
> On Thu, Dec 18, 2014 at 8:20 AM, Erik Cederstrand <
> [email protected]> wrote:
>>
>> Hi list,
>>
>> I'm using Django as a hub to synchronize data between various external
>> systems. To do this, I have some long-running Django management commands
>> that are started as cron jobs. To ensure that only one job is working on a
>> data set at any one time, I have implemented a locking system using the
>> Django cache system.
>>
>> Due to my less-than-stellar programming talent, a cron job may need to be
>> killed once in a while. To avoid leaving orphaned locks in the cache, I
>> want to clean up any locks before exiting, and I may not know which cache
>> keys exist at the time of exit. Therefore, I want to write a signal handler
>> that searches the Django cache for any entries that can be traced to the
>> current process (my cache keys can be used for this purpose) and delete
>> those.
>>
>> AFAICS, the Django cache API can't be used for this. There's
>> cache.clear() but I don't want to delete all cache entries. I'm using the
>> database backend, so I'm thinking I could access the database table
>> directly and issue any custom SQL on that. But it does feel hackish, so
>> maybe one of you have a better approach? Maybe I got the whole thing
>> backwards?
>>
>> Here's my cache setup from settings.py:
>>
>> CACHES = {
>>     'default': {
>>         'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
>>         'LOCATION': 'dispatch_cache',
>>         'TIMEOUT': None,
>>     }
>> }
>>
>>
>> Thanks,
>> Erik
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/9A6110DB-9D5A-4A3F-9DD3-49CC406C1C18%40cederstrand.dk
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CALn3ei0D2ZL7YJb17ibGDnBDJvTXg%2B2Hj%3DwrCZearRZBjzvpNQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CALn3ei0D2ZL7YJb17ibGDnBDJvTXg%2B2Hj%3DwrCZearRZBjzvpNQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFWa6tKpf6wEcX1bOMcvn3FVo0qXxZyQgx3djWQaMNdcRDCJQw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to