在Django请求之间共享一个连接

时间:2019-04-03 13:50:56

标签: python django redis connection-pooling

在django项目中寻找一种可靠的共享连接对象的方式,类似于django数据库连接。

# creating and entity set 
entity_set = ft.EntitySet(id = 'rem_dur')

# adding a dataframe 
entity_set.entity_from_dataframe(entity_id = 'enh', dataframe = dataset, index = 'unique_id'
,,variable_types = {'Start_Date': ft.variable_types.DatetimeTimeIndex})) 

#unique_id is just row number from 1 to number of rows in dataset


entity_set.normalize_entity(base_entity_id='enh', new_entity_id= 'categorical_vars', index = 'ITEMID', 
                             additional_variables = ['cat_var_1', 'cat_var_2'])

###cutoff date 
cutoff_df = dataset[["unique_id", "trans_date"]]
cutoff_df["trans_date"] = pd.to_datetime(cutoff_df["trans_date"])

##feature engg
feature_matrix_2, feature_names_2 = ft.dfs(entityset=entity_set
                                       ,target_entity = 'enh'
                                       ,max_depth = 2
                                       ,verbose = 1 
                                       ,ignore_entities = ['categorical_vars']
                                       ,ignore_variables =ignore_features_dict
                                       ,dask_kwargs={'cluster': cluster}
                                       ,cutoff_time=cutoff_df
                                      ,cutoff_time_in_index=False
                                       )

It's unable to generate any time series features. It's returning just all the features except the ones which are ignored.

不一定使用相同的API,但是我需要类似的方式来获取redis连接池,该连接池在django服务器启动时初始化,因此我可以重用项目不同部分中的单个连接池。

您有什么建议或想法,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在Django CacheHandler

中找到了对我有用的解决方案

在这种情况下,使用REDIS连接redis-sentinel-url

settings.py-

中的Redis连接字符串定义
REDIS = {
    "default": "redis:///",
    "secondary": "redis+sentinel:///"
}

RedisPoolHandler类实现类似于django CacheHandler

from threading import local

import redis_sentinel_url
from django.conf import settings
from django.core.cache import InvalidCacheBackendError


class RedisClientHandler:
    """
    A Redis Client Handler to manage access to Redis instances.
    Ensure only one instance of each alias exists per thread.
    """
    def __init__(self):
        self._clients = local()

    def __getitem__(self, alias):
        try:
            return self._redis_clients.caches[alias]
        except AttributeError:
            self._redis_clients.caches = {}
        except KeyError:
            pass

        if alias not in settings.REDIS:
            raise InvalidCacheBackendError(
                "Could not find config for '%s' in settings.REDIS" % alias
            )

        value = settings.REDIS[alias]
        sentinel, redis_pool = redis_sentinel_url.connect(value, client_options={"decode_responses": True})

        self._redis_clients.caches[alias] = redis_pool
        return redis_pool

    def all(self):
        return getattr(self._redis_clients, '_redis_clients', {}).values()


redis_clients = RedisPoolHandler()

下面的用法示例:

from path.to.classfile import redis_clients

redis_client = redis_clients['default']