librelist archives

« back to archive

Request context not available in MethodView

Request context not available in MethodView

From:
me@shamail.in
Date:
2012-10-25 @ 09:43
Hi,

I've 2 queries.

1. I've code like this:

class Index (MethodView):

    def get (self):
        name = "yahoo"
        # global request
        print request
        return render_template ('hello.jade', name=name)

index_view = Index.as_view ('index')
app.add_url_rule ('/', view_func = Index.as_view ('users'))


Here print request line is throwing an exception (while requesting / from
the browser).
The commented line isn't helping either.

Why the request object isn't available in this?

2. My to-be application is going to use MongoDB (using mongoengine), now
one of the primary requirements of pymongo is that each thread should
authenticate with Mongo individually, and I obviously don't want to
handshake with database server on each and every request.
Earlier in Django, I use to connect with Mongo in the settings.py so that
the connection stays alive till the lifetime of the process, and at the
front end, I've spawning creating the processes and keep them alive for,
say 30 seconds before it gracefully kills it and starts its replacement.
(Please note, i had threading disabled in spawning, only processes).

Now since flask internally depends on threads (which I think isn't a good
design decision either because of GIL in python), is there a way, that
there are no threads in the system at all so that I can have long lived
mongoengine connections? Or if there is none, is it possible to have the
connection for the lifetime of the thread?

Please help me with these to choose flask in an upcoming app..

Tx


-- 
Shamail Tayyab

Re: [flask] Request context not available in MethodView

From:
Audrius Kažukauskas
Date:
2012-10-25 @ 11:49
On Thu, 2012-10-25 at 15:13:52 +0530, me@shamail.in wrote:
> 1. I've code like this:
> 
> class Index (MethodView):
> 
>     def get (self):
>         name = "yahoo"
>         # global request
>         print request
>         return render_template ('hello.jade', name=name)
> 
> index_view = Index.as_view ('index')
> app.add_url_rule ('/', view_func = Index.as_view ('users'))
> 
> 
> Here print request line is throwing an exception (while requesting / from
> the browser).
> The commented line isn't helping either.

Without seeing the exception you get it's hard to tell what's wrong, but
shouldn't the last two lines in your code be:

  index_view = Index.as_view('index')
  app.add_url_rule('/', view_func=index_view)

Commented line (global statement) is not needed to access request,
its purpose is to let you assign to a global variable inside a function
or a method.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Request context not available in MethodView

From:
me@shamail.in
Date:
2012-10-25 @ 12:17
I think I found out the solutions..

On Thu, Oct 25, 2012 at 5:19 PM, Audrius Kažukauskas <audrius@neutrino.lt>wrote:

> On Thu, 2012-10-25 at 15:13:52 +0530, me@shamail.in wrote:
> > 1. I've code like this:
> >
> > class Index (MethodView):
> >
> >     def get (self):
> >         name = "yahoo"
> >         # global request
> >         print request
> >         return render_template ('hello.jade', name=name)
> >
> > index_view = Index.as_view ('index')
> > app.add_url_rule ('/', view_func = Index.as_view ('users'))
> >
> >
> > Here print request line is throwing an exception (while requesting / from
> > the browser).
> > The commented line isn't helping either.
>
> Without seeing the exception you get it's hard to tell what's wrong, but
> shouldn't the last two lines in your code be:
>
>   index_view = Index.as_view('index')
>   app.add_url_rule('/', view_func=index_view)
>
> Commented line (global statement) is not needed to access request,
> its purpose is to let you assign to a global variable inside a function
> or a method.
>
> --
> Audrius Kažukauskas
> http://neutrino.lt/
>

Ya, thats like a typo.. I didn't had request imported in the current file
;-) I was relying on a * import from another file.

And to the second problem, yes, I tested myself with spawning, flask is
indeed working without threads after eventlets monkey patching, also
mongoengine is working well too.

Now I am tempted to ditch django this time ;-) Lets see..

-- 
Shamail Tayyab

Re: [flask] Request context not available in MethodView

From:
Simon Sapin
Date:
2012-10-26 @ 10:38
Le 25/10/2012 14:17, me@shamail.in a écrit :
> And to the second problem, yes, I tested myself with spawning, flask is
> indeed working without threads after eventlets monkey patching

Yes, you can use processes, threads, greenlets, or whatever you like. 
Flask is designed to work in all these cases.

You might have been thinking of "thread-local / context-local objects" 
that Flask uses:

http://flask.pocoo.org/docs/design/#thread-locals
http://flask.pocoo.org/docs/advanced_foreword/#thread-locals-in-flask

Cheers,
-- 
Simon Sapin