librelist archives

« back to archive

Using Flask-SQLAlchemy outside of a request

Using Flask-SQLAlchemy outside of a request

From:
Ian Langworth
Date:
2011-03-31 @ 15:21
My Flask app is up and running nicely, but I'd like to also use my
Flask-SQLAlchemy models in a long-running non-Flask application —
specifically a socket.io listener using TornadIO. Unfortunately, after
a while I start to get errors:

    InvalidRequestError: Can't reconnect until invalid transaction is
rolled back

This is undoubtedly due to me naively using the model classes without
managing the session.

Does anyone have a snippet of code showing how to use the
Flask-SQLAlchemy session outside the context of a request? Is it as
easy as calling db.session.close() in a try/finally around my queries?
I'm guessing it would be used a little differently than the regular
SQLAlchemy session.

Re: [flask] Using Flask-SQLAlchemy outside of a request

From:
Kates Gasis
Date:
2011-03-31 @ 15:31
ctx = app.test_request_context()
ctx.push()

... do your model procedures here...

ctx.pop()


On 31-Mar-2011, at 11:21 PM, Ian Langworth wrote:

> My Flask app is up and running nicely, but I'd like to also use my
> Flask-SQLAlchemy models in a long-running non-Flask application —
> specifically a socket.io listener using TornadIO. Unfortunately, after
> a while I start to get errors:
> 
>    InvalidRequestError: Can't reconnect until invalid transaction is
> rolled back
> 
> This is undoubtedly due to me naively using the model classes without
> managing the session.
> 
> Does anyone have a snippet of code showing how to use the
> Flask-SQLAlchemy session outside the context of a request? Is it as
> easy as calling db.session.close() in a try/finally around my queries?
> I'm guessing it would be used a little differently than the regular
> SQLAlchemy session.

Re: [flask] Using Flask-SQLAlchemy outside of a request

From:
Ian Langworth
Date:
2011-04-02 @ 05:19
Thanks! I gave that a try, but no luck.

I'm going to dive into the source & docs now. I'd welcome any pointers
as to where to start debugging if anyone as any.

On Thu, Mar 31, 2011 at 08:31, Kates Gasis <katesgasis@gmail.com> wrote:
> ctx = app.test_request_context()
> ctx.push()
>
> ... do your model procedures here...
>
> ctx.pop()
>
>
> On 31-Mar-2011, at 11:21 PM, Ian Langworth wrote:
>
>> My Flask app is up and running nicely, but I'd like to also use my
>> Flask-SQLAlchemy models in a long-running non-Flask application —
>> specifically a socket.io listener using TornadIO. Unfortunately, after
>> a while I start to get errors:
>>
>>    InvalidRequestError: Can't reconnect until invalid transaction is
>> rolled back
>>
>> This is undoubtedly due to me naively using the model classes without
>> managing the session.
>>
>> Does anyone have a snippet of code showing how to use the
>> Flask-SQLAlchemy session outside the context of a request? Is it as
>> easy as calling db.session.close() in a try/finally around my queries?
>> I'm guessing it would be used a little differently than the regular
>> SQLAlchemy session.
>
>

Re: [flask] Using Flask-SQLAlchemy outside of a request

From:
Rob Mela
Date:
2011-04-03 @ 22:22
Why not just import and use the models directly, w/o Flask?

I'm probably exposing my ignorance about SQLAlchemy-Flask.   But I've had 
no problem using SQLAlchemy models in standalone, back-end scripts.

Den Apr 2, 2011 kl. 1:19 AM skrev Ian Langworth:

> Thanks! I gave that a try, but no luck.
> 
> I'm going to dive into the source & docs now. I'd welcome any pointers
> as to where to start debugging if anyone as any.
> 
> On Thu, Mar 31, 2011 at 08:31, Kates Gasis <katesgasis@gmail.com> wrote:
>> ctx = app.test_request_context()
>> ctx.push()
>> 
>> ... do your model procedures here...
>> 
>> ctx.pop()
>> 
>> 
>> On 31-Mar-2011, at 11:21 PM, Ian Langworth wrote:
>> 
>>> My Flask app is up and running nicely, but I'd like to also use my
>>> Flask-SQLAlchemy models in a long-running non-Flask application —
>>> specifically a socket.io listener using TornadIO. Unfortunately, after
>>> a while I start to get errors:
>>> 
>>>    InvalidRequestError: Can't reconnect until invalid transaction is
>>> rolled back
>>> 
>>> This is undoubtedly due to me naively using the model classes without
>>> managing the session.
>>> 
>>> Does anyone have a snippet of code showing how to use the
>>> Flask-SQLAlchemy session outside the context of a request? Is it as
>>> easy as calling db.session.close() in a try/finally around my queries?
>>> I'm guessing it would be used a little differently than the regular
>>> SQLAlchemy session.
>> 
>>