librelist archives

« back to archive

A very simple question

A very simple question

From:
eduardo
Date:
2011-01-20 @ 21:19
Hi,

everywhere I see code like this:


import sqlite3
from  flask  import  g

DATABASE  =  '/path/to/database.db'

def  connect_db():
     return  sqlite3.connect(DATABASE)

@app.before_request
def  before_request():
     g.db  =  connect_db()

@app.after_request
def  after_request(response):
     g.db.close()
     return  response


Ok, I now that *g* is global (kind of). But in *g.db
*the *db* portion comes from where? Where was
it assigned?
*

*regards,
*
*eduardo

Re: [flask] A very simple question

From:
ChiliConSQL
Date:
2011-01-20 @ 21:42
It is assigned right here:

def before_request():
    *g.db* = connect_db()



On Thu, Jan 20, 2011 at 4:19 PM, eduardo <eduardo.manso@gmail.com> wrote:

>  Hi,
>
> everywhere I see code like this:
>
>
> import sqlite3
> from flask import g
> DATABASE = '/path/to/database.db'
> def connect_db():
>     return sqlite3.connect(DATABASE)
> @app.before_requestdef before_request():
>     g.db = connect_db()
> @app.after_requestdef after_request(response):
>     g.db.close()
>     return response
>
>
> Ok, I now that *g* is global (kind of). But in *g.db
> *the *db* portion comes from where? Where was
> it assigned?
> *
>
> *regards,
> *
> *eduardo
>
>

A very simple question

From:
eduardo
Date:
2011-01-20 @ 21:26
Hi,

sorry, what i really want to now is in what moment
*g* becomes available?

*
*regards,
*
*eduardo

Re: [flask] A very simple question

From:
Armin Ronacher
Date:
2011-01-21 @ 00:35
Hi,

On 1/20/11 10:26 PM, eduardo wrote:
> sorry, what i really want to now is in what moment
> *g* becomes available?
 From the moment a request context is bound to the moment it becomes 
unbound.  By default this happens as soon as the request handling starts 
and ends as soon as the application returns:

     def wsgi_app(self, environ, start_response):
         with self.request_context(environ):
             # everything here has access to g / request etc.
             try:
                 request_started.send(self)
                 rv = self.preprocess_request()
                 if rv is None:
                     rv = self.dispatch_request()
                 response = self.make_response(rv)
             except Exception, e:
                 response = self.make_response(self.handle_exception(e))
             try:
                 response = self.process_response(response)
             except Exception, e:
                 response = self.make_response(self.handle_exception(e))
             request_finished.send(self, response=response)
             return response(environ, start_response)


Regards,
Armin

Re: [flask] A very simple question

From:
eduardo
Date:
2011-01-21 @ 12:38
Hi,


Em 20/1/2011 22:35, Armin Ronacher escreveu:
> Hi,
>
> On 1/20/11 10:26 PM, eduardo wrote:
>> sorry, what i really want to now is in what moment
>> *g* becomes available?
>    From the moment a request context is bound to the moment it becomes
> unbound.  By default this happens as soon as the request handling starts
> and ends as soon as the application returns:
>
>       def wsgi_app(self, environ, start_response):
>           with self.request_context(environ):
>               # everything here has access to g / request etc.
>               try:
>                   request_started.send(self)

that is what I needed to now.

Thanks.


regards,

  eduardo

Re: [flask] A very simple question

From:
Peter Ward
Date:
2011-01-20 @ 21:31
You've probably answered your g.db question by searching for "g.db =". :)

*g* is a per-request object which stores global variables.
It would therefore be available at any time during a request.

http://flask.pocoo.org/docs/api/#application-globals

On 21 January 2011 08:26, eduardo <eduardo.manso@gmail.com> wrote:

>  Hi,
>
> sorry, what i really want to now is in what moment
> *g* becomes available?
>
> *
> *regards,
> *
> *eduardo
>



-- 
Peter Ward
http://flowblok.id.au/
BCST III, Sydney University

Re: [flask] A very simple question

From:
eduardo
Date:
2011-01-20 @ 21:42
Yes, I just read this...

Thanks,

eduardo



Em 20/1/2011 19:31, Peter Ward escreveu:
> You've probably answered your g.db question by searching for "g.db =". :)
>
> *g* is a per-request object which stores global variables.
> It would therefore be available at any time during a request.
>
> http://flask.pocoo.org/docs/api/#application-globals
>
> On 21 January 2011 08:26, eduardo <eduardo.manso@gmail.com 
> <mailto:eduardo.manso@gmail.com>> wrote:
>
>     Hi,
>
>     sorry, what i really want to now is in what moment
>     *g* becomes available?
>
>     *
>     *regards,
>     *
>     *eduardo
>
>
>
>
> -- 
> Peter Ward
> http://flowblok.id.au/
> BCST III, Sydney University

Re: [flask] A very simple question

From:
JimG
Date:
2011-01-20 @ 21:34
when it's assigned?

On 20 January 2011 21:31, Peter Ward <peteraward@gmail.com> wrote:

> You've probably answered your g.db question by searching for "g.db =". :)
>
> *g* is a per-request object which stores global variables.
> It would therefore be available at any time during a request.
>
> http://flask.pocoo.org/docs/api/#application-globals
>
>
> On 21 January 2011 08:26, eduardo <eduardo.manso@gmail.com> wrote:
>
>>  Hi,
>>
>> sorry, what i really want to now is in what moment
>> *g* becomes available?
>>
>> *
>> *regards,
>> *
>> *eduardo
>>
>
>
>
> --
> Peter Ward
> http://flowblok.id.au/
> BCST III, Sydney University
>