On Mon, Oct 7, 2013 at 12:01 PM, Jonathan Vanasco <jonat...@findmeon.com> wrote:
> so the popular workaround(s) are to do both:
>
> 1- defer to thread (which most people would do anyways)
> 2- open/close multiple sessions (which are cheap).  basically , instead of
> approaching a "task" in twisted as a single action of multiple components,
> think of it like  a website , with each action as a new page request.  your
> "web session" is the same, but your "SqlAlchemy Session" changes on each
> request.


There's a 3.

3- open a single request-scope session, and carry it through your async tasks.

You'll have to be super-careful not to use any thread-local state,
always use the request-scope session, but that's relatively easy with
most frameworks. Probably the easiest way would be to instantiate an
explicit session with a session_maker, and then carry it implicitly in
your callbacks as a closure. This is even easier on web frameworks,
that support this kind of stuff built-in since it's so very common.

But, adapting, got_api:

   def process_request():
        d = deferToThread( get_api, session_maker() ).addCallback(got_api)

   def get_api(session)
       # use session
       <block>

   def got_api():
       process data

At first sight, this doesn't look all that better, but when you start
chaining deferreds, it will be.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to