#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 Wind River Systems, Inc.
+# Copyright (c) 2017-2018 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
import redis
-from multivimbroker.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD
+from multivimbroker.pub.config.config import REDIS_HOST
+from multivimbroker.pub.config.config import REDIS_PORT
+from multivimbroker.pub.config.config import REDIS_PASSWD
class SharedLock:
- def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWD, db=9, lock_timeout=5 * 60):
+ def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT,
+ password=REDIS_PASSWD, db=9, lock_timeout=5 * 60):
self.lock_key = lock_key
self.lock_timeout = lock_timeout
- self.redis = redis.Redis(host=host, port=port, db=db, password=password)
+ self.redis = redis.Redis(host=host, port=port,
+ db=db, password=password)
self.acquire_time = -1
def acquire(self):
begin = now = int(time.time())
while (now - begin) < self.lock_timeout:
- result = self.redis.setnx(self.lock_key, now + self.lock_timeout + 1)
+ result = self.redis.setnx(self.lock_key, now +
+ self.lock_timeout + 1)
if result == 1 or result is True:
self.acquire_time = now
return True
current_lock_timestamp = int(current_lock_timestamp)
if now > current_lock_timestamp:
- next_lock_timestamp = self.redis.getset(self.lock_key, now + self.lock_timeout + 1)
+ next_lock_timestamp = self.redis.getset(self.lock_key, now +
+ self.lock_timeout + 1)
if not next_lock_timestamp:
time.sleep(1)
continue