librelist archives

« back to archive

best location for a global database connection?

best location for a global database connection?

From:
Ryan Cole
Date:
2010-09-07 @ 00:51
Where is the best place to define my global database connection object? I
was trying to just put it in the outter-most __init__.py, prior to creating
the Flask "app" object. Then I was going to import it within my Modules, but
they don't seem to be able to import the object.

I saw that the docs suggest storing it on the proxy "g" object. Is this
suggested? If I set g to my database connection object, that would mean it's
unable to be used for other things, though, right?

Ryan

Re: [flask] best location for a global database connection?

From:
Waldemar Osuch
Date:
2010-09-07 @ 02:10
On Mon, Sep 6, 2010 at 18:51, Ryan Cole <betawarz@gmail.com> wrote:
> Where is the best place to define my global database connection object? I
> was trying to just put it in the outter-most __init__.py, prior to creating
> the Flask "app" object. Then I was going to import it within my Modules, but
> they don't seem to be able to import the object.
> I saw that the docs suggest storing it on the proxy "g" object. Is this
> suggested? If I set g to my database connection object, that would mean it's
> unable to be used for other things, though, right?
> Ryan

I do not know if having a global database connection is the right solution,
but you should be able to make it work without "g" object if you shuffle
imports around.

http://dpaste.org/ivYw/

Waldemar

Re: [flask] best location for a global database connection?

From:
Thadeus Burgess
Date:
2010-09-07 @ 01:34
g.db = mydatabase()

--
Thadeus





On Mon, Sep 6, 2010 at 7:51 PM, Ryan Cole <betawarz@gmail.com> wrote:
> Where is the best place to define my global database connection object? I
> was trying to just put it in the outter-most __init__.py, prior to creating
> the Flask "app" object. Then I was going to import it within my Modules, but
> they don't seem to be able to import the object.
> I saw that the docs suggest storing it on the proxy "g" object. Is this
> suggested? If I set g to my database connection object, that would mean it's
> unable to be used for other things, though, right?
> Ryan

Re: [flask] best location for a global database connection?

From:
Ryan Cole
Date:
2010-09-07 @ 01:56
Hrm. This is the issue I was running into earlier when I was trying to first
use g.

2010-09-06 20:54:53 [23562] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File

"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/arbiter.py",
line 422, in spawn_worker
    worker.init_process()
  File

"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/workers/base.py",
line 95, in init_process
    self.wsgi = self.app.wsgi()
  File

"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/app/base.py",
line 101, in wsgi
    self.callable = self.load()
  File

"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/app/wsgiapp.py",
line 24, in load
    return util.import_app(self.app_uri)
  File

"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/util.py",
line 178, in import_app
    __import__(module)
  File "/home/ryan/sites/wowscouter/wowscouter/__init__.py", line 13, in
<module>
    g.database = Connection()['wowscouter']
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/local.py", line 361,
in <lambda>
    __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/local.py", line 306,
in _get_current_object
    return self.__local()
  File "/usr/local/lib/python2.6/dist-packages/flask/globals.py", line 20,
in <lambda>
    g = LocalProxy(lambda: _request_ctx_stack.top.g)
AttributeError: 'NoneType' object has no attribute 'g'

The code for my __init__.py, where this is taking place, looks like this:
http://dpaste.org/ohSA/

Am I doing this wrong?

Ryan

On Mon, Sep 6, 2010 at 8:34 PM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:

> g.db = mydatabase()
>
> --
> Thadeus
>
>
>
>
>
> On Mon, Sep 6, 2010 at 7:51 PM, Ryan Cole <betawarz@gmail.com> wrote:
> > Where is the best place to define my global database connection object? I
> > was trying to just put it in the outter-most __init__.py, prior to
> creating
> > the Flask "app" object. Then I was going to import it within my Modules,
> but
> > they don't seem to be able to import the object.
> > I saw that the docs suggest storing it on the proxy "g" object. Is this
> > suggested? If I set g to my database connection object, that would mean
> it's
> > unable to be used for other things, though, right?
> > Ryan
>

Re: [flask] best location for a global database connection?

From:
Thadeus Burgess
Date:
2010-09-07 @ 02:02
They are only accessible during the lifetime of the request. You
cannot access these objects outside of the request.

You should place it in

@app.before_request()
def func():
  g.db = database()

--
Thadeus





On Mon, Sep 6, 2010 at 8:56 PM, Ryan Cole <betawarz@gmail.com> wrote:
> Hrm. This is the issue I was running into earlier when I was trying to first
> use g.
> 2010-09-06 20:54:53 [23562] [ERROR] Exception in worker process:
> Traceback (most recent call last):
>   File
> 
"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/arbiter.py",
> line 422, in spawn_worker
>     worker.init_process()
>   File
> 
"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/workers/base.py",
> line 95, in init_process
>     self.wsgi = self.app.wsgi()
>   File
> 
"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/app/base.py",
> line 101, in wsgi
>     self.callable = self.load()
>   File
> 
"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/app/wsgiapp.py",
> line 24, in load
>     return util.import_app(self.app_uri)
>   File
> 
"/usr/local/lib/python2.6/dist-packages/gunicorn-0.10.0-py2.6.egg/gunicorn/util.py",
> line 178, in import_app
>     __import__(module)
>   File "/home/ryan/sites/wowscouter/wowscouter/__init__.py", line 13, in
> <module>
>     g.database = Connection()['wowscouter']
>   File "/usr/local/lib/python2.6/dist-packages/werkzeug/local.py", line 361,
> in <lambda>
>     __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
>   File "/usr/local/lib/python2.6/dist-packages/werkzeug/local.py", line 306,
> in _get_current_object
>     return self.__local()
>   File "/usr/local/lib/python2.6/dist-packages/flask/globals.py", line 20,
> in <lambda>
>     g = LocalProxy(lambda: _request_ctx_stack.top.g)
> AttributeError: 'NoneType' object has no attribute 'g'
> The code for my __init__.py, where this is taking place, looks like
> this: http://dpaste.org/ohSA/
> Am I doing this wrong?
> Ryan
> On Mon, Sep 6, 2010 at 8:34 PM, Thadeus Burgess <thadeusb@thadeusb.com>
> wrote:
>>
>> g.db = mydatabase()
>>
>> --
>> Thadeus
>>
>>
>>
>>
>>
>> On Mon, Sep 6, 2010 at 7:51 PM, Ryan Cole <betawarz@gmail.com> wrote:
>> > Where is the best place to define my global database connection object?
>> > I
>> > was trying to just put it in the outter-most __init__.py, prior to
>> > creating
>> > the Flask "app" object. Then I was going to import it within my Modules,
>> > but
>> > they don't seem to be able to import the object.
>> > I saw that the docs suggest storing it on the proxy "g" object. Is this
>> > suggested? If I set g to my database connection object, that would mean
>> > it's
>> > unable to be used for other things, though, right?
>> > Ryan
>
>