<commit_before>from __future__ import absolute_import

import logging
import redis

from contextlib import contextmanager
from random import random
from time import sleep

from .container import Container


class UnableToGetLock(Exception):
    pass


class _Redis(object):
    def __init__(self, app, options):
        self.app = app
        self.redis = redis.from_url(app.config['REDIS_URL'])
        self.logger = logging.getLogger(app.name + '.redis')

    def __getattr__(self, name):
        return getattr(self.redis, name)

    @contextmanager
    def lock(self, lock_key, timeout=3, nowait=False):
        conn = self.redis

        delay = 0.01 + random() / 10
        attempt = 0
        max_attempts = timeout / delay
        got_lock = None
        while not got_lock and attempt < max_attempts:
            pipe = conn.pipeline()
            pipe.setnx(lock_key, '')
            pipe.expire(lock_key, timeout)
            got_lock = pipe.execute()[0]
            if not got_lock:
                if nowait:
                    break
                sleep(delay)
                attempt += 1

        self.logger.info('Acquiring lock on %s', lock_key)

        if not got_lock:
            raise UnableToGetLock('Unable to fetch lock after on %s' % (lock_key,))

        try:
            yield
        finally:
            self.logger.info('Releasing on %s', lock_key)

            try:
                redis.delete(lock_key)
            except Exception as e:
                self.logger.exception(e)


Redis = lambda **o: Container(_Redis, o, name='redis')<commit_msg>Fix for key deletion<commit_after>from __future__ import absolute_import

import logging
import redis

from contextlib import contextmanager
from random import random
from time import sleep

from .container import Container


class UnableToGetLock(Exception):
    pass


class _Redis(object):
    def __init__(self, app, options):
        self.app = app
        self.redis = redis.from_url(app.config['REDIS_URL'])
        self.logger = logging.getLogger(app.name + '.redis')

    def __getattr__(self, name):
        return getattr(self.redis, name)

    @contextmanager
    def lock(self, lock_key, timeout=3, nowait=False):
        conn = self.redis

        delay = 0.01 + random() / 10
        attempt = 0
        max_attempts = timeout / delay
        got_lock = None
        while not got_lock and attempt < max_attempts:
            pipe = conn.pipeline()
            pipe.setnx(lock_key, '')
            pipe.expire(lock_key, timeout)
            got_lock = pipe.execute()[0]
            if not got_lock:
                if nowait:
                    break
                sleep(delay)
                attempt += 1

        self.logger.info('Acquiring lock on %s', lock_key)

        if not got_lock:
            raise UnableToGetLock('Unable to fetch lock after on %s' % (lock_key,))

        try:
            yield
        finally:
            self.logger.info('Releasing on %s', lock_key)

            try:
                conn.delete(lock_key)
            except Exception as e:
                self.logger.exception(e)


Redis = lambda **o: Container(_Redis, o, name='redis')
{'fraction_non_alphanumeric': 0.08947700063011972, 'fraction_numerical': 0.005671077504725898, 'mean_word_length': 1.9562383612662941, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6773368', 'n_tokens_mistral': 946, 'n_tokens_neox': 896, 'n_words': 250}