librelist archives

« back to archive

app context with configuration data from database

app context with configuration data from database

From:
Cornelius Kölbel
Date:
2014-11-12 @ 10:11
Hi,

I have an application that stores its own configuration in a database table.
As each request needs the complete configuration and I would like to
avoid reading the configuration from the database at each request, I
would like to store the configuration in the app context and have it
available at each request. Is this possible?

I took a look at
http://flask.pocoo.org/docs/0.10/appcontext/#context-usage but run into
errors.
Obviously I have to pass a werkzeug "Local" object to the proxy but I
end up with any kind of errors (see below).

    from werkzeug.local import Local, LocalProxy

    class GetConfigFromDB(Local):
        info = "Test"


    def get_config():
        '''
        return the thread local dict with all entries

        :return: local config
        :rtype: Local
        '''
        pi_config = getattr(g, '_pi_config', None)
        if pi_config is None:
            pi_config = g._pi_config = GetConfigFromDB()
        return pi_config

    my_config = LocalProxy(get_config)

What do you think - is this the right way to go?
Are there any more detailed examples of adding such a config object to
the app context?

Thanks a lot and kind regards
Cornelius

--snip--
Traceback (most recent call last):
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
line 827, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/test.py", line
855, in run_wsgi_app
    app_iter = app(environ, start_response)
  File
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/local.py",
line 366, in <lambda>
    __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
TypeError: __call__() takes exactly 2 arguments (3 given)

Re: [flask] app context with configuration data from database

From:
Moritz Beber
Date:
2014-11-12 @ 13:23
Not sure what you are trying to accomplish but would it make sense to load
the configuration values from the database and store them in the flask app
config attribute before calling the app's run method? If this doesn't suit
your need maybe use a cache, e.g., with a dict or redis, from which you
load the configuration values without polling the database.

On Wed, Nov 12, 2014 at 11:11 AM, Cornelius Kölbel <
cornelius@privacyidea.org> wrote:

>  Hi,
>
> I have an application that stores its own configuration in a database
> table.
> As each request needs the complete configuration and I would like to avoid
> reading the configuration from the database at each request, I would like
> to store the configuration in the app context and have it available at each
> request. Is this possible?
>
> I took a look at
> http://flask.pocoo.org/docs/0.10/appcontext/#context-usage but run into
> errors.
> Obviously I have to pass a werkzeug "Local" object to the proxy but I end
> up with any kind of errors (see below).
>
> from werkzeug.local import Local, LocalProxy
>
> class GetConfigFromDB(Local):
>     info = "Test"
>
>
> def get_config():
>     '''
>     return the thread local dict with all entries
>
>     :return: local config
>     :rtype: Local
>     '''
>     pi_config = getattr(g, '_pi_config', None)
>     if pi_config is None:
>         pi_config = g._pi_config = GetConfigFromDB()
>     return pi_config
>
> my_config = LocalProxy(get_config)
>
>  What do you think - is this the right way to go?
> Are there any more detailed examples of adding such a config object to the
> app context?
>
> Thanks a lot and kind regards
> Cornelius
>
> --snip--
> Traceback (most recent call last):
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1836, in __call__
>     return self.wsgi_app(environ, start_response)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1820, in wsgi_app
>     response = self.make_response(self.handle_exception(e))
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1403, in handle_exception
>     reraise(exc_type, exc_value, tb)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1817, in wsgi_app
>     response = self.full_dispatch_request()
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1478, in full_dispatch_request
>     response = self.make_response(rv)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
> 1577, in make_response
>     rv = self.response_class.force_type(rv, request.environ)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
> line 827, in force_type
>     response = BaseResponse(*_run_wsgi_app(response, environ))
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
> line 57, in _run_wsgi_app
>     return _run_wsgi_app(*args)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/test.py",
> line 855, in run_wsgi_app
>     app_iter = app(environ, start_response)
>   File
> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/local.py",
> line 366, in <lambda>
>     __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
> TypeError: __call__() takes exactly 2 arguments (3 given)
>
>
>

Re: [flask] app context with configuration data from database

From:
Cornelius Kölbel
Date:
2014-11-12 @ 14:41
I want to avoid reading the data from the database all over again.
There are about 50 to 100 user configured config data, that can indeed
change during runtime.

So to "chache" such data between requests during the lifecycle of the
app, the current_app.config would be the right place - not "g"?

Am 12.11.2014 um 14:23 schrieb Moritz Beber:
> Not sure what you are trying to accomplish but would it make sense to
> load the configuration values from the database and store them in the
> flask app config attribute before calling the app's run method? If
> this doesn't suit your need maybe use a cache, e.g., with a dict or
> redis, from which you load the configuration values without polling
> the database.
> I
> On Wed, Nov 12, 2014 at 11:11 AM, Cornelius Kölbel
> <cornelius@privacyidea.org <mailto:cornelius@privacyidea.org>> wrote:
>
>     Hi,
>
>     I have an application that stores its own configuration in a
>     database table.
>     As each request needs the complete configuration and I would like
>     to avoid reading the configuration from the database at each
>     request, I would like to store the configuration in the app
>     context and have it available at each request. Is this possible?
>
>     I took a look at
>     http://flask.pocoo.org/docs/0.10/appcontext/#context-usage but run
>     into errors.
>     Obviously I have to pass a werkzeug "Local" object to the proxy
>     but I end up with any kind of errors (see below).
>
>         from werkzeug.local import Local, LocalProxy
>
>         class GetConfigFromDB(Local):
>             info = "Test"
>
>
>         def get_config():
>             '''
>             return the thread local dict with all entries
>
>             :return: local config
>             :rtype: Local
>             '''
>             pi_config = getattr(g, '_pi_config', None)
>             if pi_config is None:
>                 pi_config = g._pi_config = GetConfigFromDB()
>             return pi_config
>
>         my_config = LocalProxy(get_config)
>
>     What do you think - is this the right way to go?
>     Are there any more detailed examples of adding such a config
>     object to the app context?
>
>     Thanks a lot and kind regards
>     Cornelius
>
>     --snip--
>     Traceback (most recent call last):
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1836, in __call__
>         return self.wsgi_app(environ, start_response)
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1820, in wsgi_app
>         response = self.make_response(self.handle_exception(e))
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1403, in handle_exception
>         reraise(exc_type, exc_value, tb)
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1817, in wsgi_app
>         response = self.full_dispatch_request()
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1478, in full_dispatch_request
>         response = self.make_response(rv)
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py",
>     line 1577, in make_response
>         rv = self.response_class.force_type(rv, request.environ)
>       File
>     
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
>     line 827, in force_type
>         response = BaseResponse(*_run_wsgi_app(response, environ))
>       File
>     
"/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
>     line 57, in _run_wsgi_app
>         return _run_wsgi_app(*args)
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/test.py",
>     line 855, in run_wsgi_app
>         app_iter = app(environ, start_response)
>       File
>     "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/local.py",
>     line 366, in <lambda>
>         __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
>     TypeError: __call__() takes exactly 2 arguments (3 given)
>
>
>

Re: [flask] app context with configuration data from database

From:
Aaron Crowder
Date:
2014-11-12 @ 15:16
Have a look at this: http://flask.pocoo.org/snippets/73/

You'll want to do something like that to store the values in redis (or some
other cache) and read them on each request. If a value changes then flush
the cache and update it.

On Wed, Nov 12, 2014, 6:46 AM Cornelius Kölbel <cornelius@privacyidea.org>
wrote:

>  I want to avoid reading the data from the database all over again.
> There are about 50 to 100 user configured config data, that can indeed
> change during runtime.
>
> So to "chache" such data between requests during the lifecycle of the app,
> the current_app.config would be the right place - not "g"?
>
> Am 12.11.2014 um 14:23 schrieb Moritz Beber:
>
> Not sure what you are trying to accomplish but would it make sense to load
> the configuration values from the database and store them in the flask app
> config attribute before calling the app's run method? If this doesn't suit
> your need maybe use a cache, e.g., with a dict or redis, from which you
> load the configuration values without polling the database.
>  I
> On Wed, Nov 12, 2014 at 11:11 AM, Cornelius Kölbel <
> cornelius@privacyidea.org> wrote:
>
>>  Hi,
>>
>> I have an application that stores its own configuration in a database
>> table.
>> As each request needs the complete configuration and I would like to
>> avoid reading the configuration from the database at each request, I would
>> like to store the configuration in the app context and have it available at
>> each request. Is this possible?
>>
>> I took a look at
>> http://flask.pocoo.org/docs/0.10/appcontext/#context-usage but run into
>> errors.
>> Obviously I have to pass a werkzeug "Local" object to the proxy but I end
>> up with any kind of errors (see below).
>>
>> from werkzeug.local import Local, LocalProxy
>>
>> class GetConfigFromDB(Local):
>>     info = "Test"
>>
>>
>> def get_config():
>>     '''
>>     return the thread local dict with all entries
>>
>>     :return: local config
>>     :rtype: Local
>>     '''
>>     pi_config = getattr(g, '_pi_config', None)
>>     if pi_config is None:
>>         pi_config = g._pi_config = GetConfigFromDB()
>>     return pi_config
>>
>> my_config = LocalProxy(get_config)
>>
>>  What do you think - is this the right way to go?
>> Are there any more detailed examples of adding such a config object to
>> the app context?
>>
>> Thanks a lot and kind regards
>> Cornelius
>>
>> --snip--
>> Traceback (most recent call last):
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1836, in __call__
>>     return self.wsgi_app(environ, start_response)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1820, in wsgi_app
>>     response = self.make_response(self.handle_exception(e))
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1403, in handle_exception
>>     reraise(exc_type, exc_value, tb)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1817, in wsgi_app
>>     response = self.full_dispatch_request()
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1478, in full_dispatch_request
>>     response = self.make_response(rv)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/flask/app.py", line
>> 1577, in make_response
>>     rv = self.response_class.force_type(rv, request.environ)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
>> line 827, in force_type
>>     response = BaseResponse(*_run_wsgi_app(response, environ))
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/wrappers.py",
>> line 57, in _run_wsgi_app
>>     return _run_wsgi_app(*args)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/test.py",
>> line 855, in run_wsgi_app
>>     app_iter = app(environ, start_response)
>>   File
>> "/home/cornelius/src/flask/lib/python2.7/site-packages/werkzeug/local.py",
>> line 366, in <lambda>
>>     __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
>> TypeError: __call__() takes exactly 2 arguments (3 given)
>>
>>
>>
>