Hi all, I'm new to flask (and mostly new to web applications, for that matter). In the examples in the tutorial (and elsewhere), why do we connect to the database and later shutdown *on each request*? Why can't we have a connection open for the life of the server? Not sure how slow each connect_db() is (?), but it seems wasteful to do it on every single request. What am I missing? Thanks, and please forgive the noob question! - Matt
Perhaps this question is too basic, but I can't seem to find the answer. Can anyone help? Thanks. ________________________________ From: Matt Starcrest <matt.starcrest@yahoo.com> To: flask@librelist.com Sent: Saturday, December 17, 2011 5:49 PM Subject: [flask] [beginner]: database connections per request? Hi all, I'm new to flask (and mostly new to web applications, for that matter). In the examples in the tutorial (and elsewhere), why do we connect to the database and later shutdown *on each request*? Why can't we have a connection open for the life of the server? Not sure how slow each connect_db() is (?), but it seems wasteful to do it on every single request. What am I missing? Thanks, and please forgive the noob question! - Matt
Matt, The main reason it is done on each request is because the actual state of the web app (the Python process or thread running Flask), heavily depends on what web server you are using and various other factors. Generally, keeping state (like a database connection) around in the process between requests is a bad idea(™) for this reason. Attempting to maintain some state can become very difficult to debug, etc, even when you know all the rules for your particular setup. Creating a connection each request is much simpler, and not that much of a slowdown. If you are dealing with the number of requests where recreating it each time becomes a significant burden, then the extra complication of customizing your server/process setup and using connection pooling can become worth it. Hopefully if I am saying anything completely unreasonable someone else will correct me, but that I think that should answer your question. - Kyle On Tue, Dec 20, 2011 at 1:18 PM, Matt Starcrest <matt.starcrest@yahoo.com> wrote: > Perhaps this question is too basic, but I can't seem to find the answer. > Can anyone help? > Thanks. > ________________________________ > From: Matt Starcrest <matt.starcrest@yahoo.com> > To: flask@librelist.com > Sent: Saturday, December 17, 2011 5:49 PM > Subject: [flask] [beginner]: database connections per request? > > Hi all, > I'm new to flask (and mostly new to web applications, for that matter). > > In the examples in the tutorial (and elsewhere), why do we connect to the > database and later shutdown *on each request*? Why can't we have a > connection open for the life of the server? Not sure how slow each > connect_db() is (?), but it seems wasteful to do it on every single request. > What am I missing? > > Thanks, and please forgive the noob question! > - Matt > >
Kyle I think you may have confused database connections and database transactions. Matt - as I understand it the reason is as a webserver can be processing multiple requests concurrently, therefore more than one connection may be required. The simple solution is to create a database connection for each request and close it when done. Certain solutions, such as Java Enterprise containers actually have a database connection pool, where each requests obtains a ready open connection from the pool at the start of the request and returns it afterwards. In Python, SQLalchemy has connection pooling built in. On 20 December 2011 21:43, Kyle Jones <kyle@bucebuce.com> wrote: > Matt, > > The main reason it is done on each request is because the actual state > of the web app (the Python process or thread running Flask), heavily > depends on what web server you are using and various other factors. > Generally, keeping state (like a database connection) around in the > process between requests is a bad idea(™) for this reason. Attempting > to maintain some state can become very difficult to debug, etc, even > when you know all the rules for your particular setup. > > Creating a connection each request is much simpler, and not that much > of a slowdown. If you are dealing with the number of requests where > recreating it each time becomes a significant burden, then the extra > complication of customizing your server/process setup and using > connection pooling can become worth it. > > Hopefully if I am saying anything completely unreasonable someone else > will correct me, but that I think that should answer your question. > > - Kyle > > > > On Tue, Dec 20, 2011 at 1:18 PM, Matt Starcrest > <matt.starcrest@yahoo.com> wrote: > > Perhaps this question is too basic, but I can't seem to find the answer. > > Can anyone help? > > Thanks. > > ________________________________ > > From: Matt Starcrest <matt.starcrest@yahoo.com> > > To: flask@librelist.com > > Sent: Saturday, December 17, 2011 5:49 PM > > Subject: [flask] [beginner]: database connections per request? > > > > Hi all, > > I'm new to flask (and mostly new to web applications, for that matter). > > > > In the examples in the tutorial (and elsewhere), why do we connect to the > > database and later shutdown *on each request*? Why can't we have a > > connection open for the life of the server? Not sure how slow each > > connect_db() is (?), but it seems wasteful to do it on every single > request. > > What am I missing? > > > > Thanks, and please forgive the noob question! > > - Matt > > > > >
Kyle I just reread your response and realised I misread yours. Apologies for the accusation of confusion. On 20 December 2011 21:51, JimG <j.gumbley@gmail.com> wrote: > Kyle I think you may have confused database connections and database > transactions. > > Matt - as I understand it the reason is as a webserver can be processing > multiple requests concurrently, therefore more than one connection may be > required. The simple solution is to create a database connection for each > request and close it when done. Certain solutions, such as Java Enterprise > containers actually have a database connection pool, where each requests > obtains a ready open connection from the pool at the start of the request > and returns it afterwards. In Python, SQLalchemy has connection pooling > built in. > > On 20 December 2011 21:43, Kyle Jones <kyle@bucebuce.com> wrote: > >> Matt, >> >> The main reason it is done on each request is because the actual state >> of the web app (the Python process or thread running Flask), heavily >> depends on what web server you are using and various other factors. >> Generally, keeping state (like a database connection) around in the >> process between requests is a bad idea(™) for this reason. Attempting >> to maintain some state can become very difficult to debug, etc, even >> when you know all the rules for your particular setup. >> >> Creating a connection each request is much simpler, and not that much >> of a slowdown. If you are dealing with the number of requests where >> recreating it each time becomes a significant burden, then the extra >> complication of customizing your server/process setup and using >> connection pooling can become worth it. >> >> Hopefully if I am saying anything completely unreasonable someone else >> will correct me, but that I think that should answer your question. >> >> - Kyle >> >> >> >> On Tue, Dec 20, 2011 at 1:18 PM, Matt Starcrest >> <matt.starcrest@yahoo.com> wrote: >> > Perhaps this question is too basic, but I can't seem to find the answer. >> > Can anyone help? >> > Thanks. >> > ________________________________ >> > From: Matt Starcrest <matt.starcrest@yahoo.com> >> > To: flask@librelist.com >> > Sent: Saturday, December 17, 2011 5:49 PM >> > Subject: [flask] [beginner]: database connections per request? >> > >> > Hi all, >> > I'm new to flask (and mostly new to web applications, for that matter). >> > >> > In the examples in the tutorial (and elsewhere), why do we connect to >> the >> > database and later shutdown *on each request*? Why can't we have a >> > connection open for the life of the server? Not sure how slow each >> > connect_db() is (?), but it seems wasteful to do it on every single >> request. >> > What am I missing? >> > >> > Thanks, and please forgive the noob question! >> > - Matt >> > >> > >> > >
Usually applications use connection pooling (pymongo does this) such that there is a pool of connections that are in an equivalent to a "fresh state," and a connection is attached to each request as it comes in. Craig Younkins On Tue, Dec 20, 2011 at 4:43 PM, Kyle Jones <kyle@bucebuce.com> wrote: > Matt, > > The main reason it is done on each request is because the actual state > of the web app (the Python process or thread running Flask), heavily > depends on what web server you are using and various other factors. > Generally, keeping state (like a database connection) around in the > process between requests is a bad idea(™) for this reason. Attempting > to maintain some state can become very difficult to debug, etc, even > when you know all the rules for your particular setup. > > Creating a connection each request is much simpler, and not that much > of a slowdown. If you are dealing with the number of requests where > recreating it each time becomes a significant burden, then the extra > complication of customizing your server/process setup and using > connection pooling can become worth it. > > Hopefully if I am saying anything completely unreasonable someone else > will correct me, but that I think that should answer your question. > > - Kyle > > > > On Tue, Dec 20, 2011 at 1:18 PM, Matt Starcrest > <matt.starcrest@yahoo.com> wrote: > > Perhaps this question is too basic, but I can't seem to find the answer. > > Can anyone help? > > Thanks. > > ________________________________ > > From: Matt Starcrest <matt.starcrest@yahoo.com> > > To: flask@librelist.com > > Sent: Saturday, December 17, 2011 5:49 PM > > Subject: [flask] [beginner]: database connections per request? > > > > Hi all, > > I'm new to flask (and mostly new to web applications, for that matter). > > > > In the examples in the tutorial (and elsewhere), why do we connect to the > > database and later shutdown *on each request*? Why can't we have a > > connection open for the life of the server? Not sure how slow each > > connect_db() is (?), but it seems wasteful to do it on every single > request. > > What am I missing? > > > > Thanks, and please forgive the noob question! > > - Matt > > > > >
Thanks everyone. I've mostly used pymongo, so it seems I've been
unwittingly spoiled by the built-in connection pooling. With pymongo, it
sounds like it's ok to keep a persistent global connection. This isn't my
code (just came up in a google search), but it reflects the pattern I was
expecting. I'll assume this is safe, but please let me know if I'm still
misunderstanding!
http://pastebin.com/KC2buBZf
from pymongo import Connection
from flask import Flask, render_template, redirect, url_for
app = Flask(__name__)
db = Connection().paste
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
if 'code' in request.form:
id = save_paste(request.form)
return redirect(url_for('view', id=id)
return render_template('index.html')
@app.route('/<id>')
def view(id):
paste = db.pastes.findOne({'_id': id})
return render_template('view.html', paste=paste)
def save_paste(form):
return db.pastes.insert({'code': form['code']})
________________________________
From: Craig Younkins <cyounkins@gmail.com>
To: flask@librelist.com
Sent: Tuesday, December 20, 2011 1:50 PM
Subject: Re: [flask] [beginner]: database connections per request?
Usually applications use connection pooling (pymongo does this) such that
there is a pool of connections that are in an equivalent to a "fresh
state," and a connection is attached to each request as it comes in.
Craig Younkins
On Tue, Dec 20, 2011 at 4:43 PM, Kyle Jones <kyle@bucebuce.com> wrote:
Matt,
>
>The main reason it is done on each request is because the actual state
>of the web app (the Python process or thread running Flask), heavily
>depends on what web server you are using and various other factors.
>Generally, keeping state (like a database connection) around in the
>process between requests is a bad idea(™) for this reason. Attempting
>to maintain some state can become very difficult to debug, etc, even
>when you know all the rules for your particular setup.
>
>Creating a connection each request is much simpler, and not that much
>of a slowdown. If you are dealing with the number of requests where
>recreating it each time becomes a significant burden, then the extra
>complication of customizing your server/process setup and using
>connection pooling can become worth it.
>
>Hopefully if I am saying anything completely unreasonable someone else
>will correct me, but that I think that should answer your question.
>
>- Kyle
>
>
>
>
>On Tue, Dec 20, 2011 at 1:18 PM, Matt Starcrest
><matt.starcrest@yahoo.com> wrote:
>> Perhaps this question is too basic, but I can't seem to find the answer.
>> Can anyone help?
>> Thanks.
>> ________________________________
>> From: Matt Starcrest <matt.starcrest@yahoo.com>
>> To: flask@librelist.com
>> Sent: Saturday, December 17, 2011 5:49 PM
>> Subject: [flask] [beginner]: database connections per request?
>>
>> Hi all,
>> I'm new to flask (and mostly new to web applications, for that matter).
>>
>> In the examples in the tutorial (and elsewhere), why do we connect to the
>> database and later shutdown *on each request*? Why can't we have a
>> connection open for the life of the server? Not sure how slow each
>> connect_db() is (?), but it seems wasteful to do it on every single request.
>> What am I missing?
>>
>> Thanks, and please forgive the noob question!
>> - Matt
>>
>>
>
Matt, It's very rare (I can't think of any instances from my professional experience) that you'd keep your database connection open except during the duration of the query. This might be a good question for the people at one of the stack exchange sites though. On Tue, Dec 20, 2011 at 16:18, Matt Starcrest <matt.starcrest@yahoo.com> wrote: > Perhaps this question is too basic, but I can't seem to find the answer. > Can anyone help? > Thanks. > ________________________________ > From: Matt Starcrest <matt.starcrest@yahoo.com> > To: flask@librelist.com > Sent: Saturday, December 17, 2011 5:49 PM > Subject: [flask] [beginner]: database connections per request? > > Hi all, > I'm new to flask (and mostly new to web applications, for that matter). > > In the examples in the tutorial (and elsewhere), why do we connect to the > database and later shutdown *on each request*? Why can't we have a > connection open for the life of the server? Not sure how slow each > connect_db() is (?), but it seems wasteful to do it on every single request. > What am I missing? > > Thanks, and please forgive the noob question! > - Matt > >