SqlAlchemy, Pylons and pylons.database.session_context.current.clear() - performance issue

2 views
Skip to first unread message

Adam Hoscilo

unread,
Mar 23, 2007, 9:27:46 AM3/23/07
to pylons-discuss
Hi,
I use SQLAlchemy and problems with threads appeared (different data in
different requests) so I added model.session_context.current.clear()
to __before__ method of my controller. Now everything work fine but
performance dropped from about 45 req/s to about 16 req/s (ab -n 500 -
c 10)
I use Elixir.
Models are described here:
http://hoscilo.pypla.net/2007/03/19/sqlalchemy-elixir-and-pylons-round-one/

Philip Jenvey

unread,
Mar 23, 2007, 6:33:59 PM3/23/07
to pylons-...@googlegroups.com

Creating a brand new session can be a quicker operation than clearing
an old one, especially if the session was associated with many
objects. clear goes through and detaches all objects from the
session. Instead of using session.clear() in __before__, try:

del model.session_context.current

When you delete a SessionContext's session, one will be lazily
created the next time it's accessed. The del operation is roughly
equivalent to:

model.session_context.current = make_session()

Without having to import make_session.

I would be surprised if only clear() was making such a significant
difference in your app's req/s, but it's a possibility depending on
what your app is doing. Let us know the details of what you find

--
Philip Jenvey


Adam Hoscilo

unread,
Mar 23, 2007, 8:46:53 PM3/23/07
to pylons-discuss

On Mar 23, 11:33 pm, Philip Jenvey <pjen...@groovie.org> wrote:
> On Mar 23, 2007, at 6:27 AM, Adam Hoscilo wrote:
> > Hi,
> > I use SQLAlchemy and problems with threads appeared (different data in
> > different requests) so I added model.session_context.current.clear()
> > to __before__ method of my controller. Now everything work fine but
> > performance dropped from about 45 req/s to about 16 req/s (ab -n 500 -
> > c 10)
> > I use Elixir.
> > Models are described here:
> >http://hoscilo.pypla.net/2007/03/19/sqlalchemy-elixir-and-pylons-
> > round-one/
>
> Creating a brand new session can be a quicker operation than clearing
> an old one, especially if the session was associated with many
> objects. clear goes through and detaches all objects from the
> session. Instead of using session.clear() in __before__, try:
>
> del model.session_context.current

Ok I'll try it first thing on Monday.

> When you delete a SessionContext's session, one will be lazily
> created the next time it's accessed. The del operation is roughly
> equivalent to:
>
> model.session_context.current = make_session()
>
> Without having to import make_session.

> I would be surprised if only clear() was making such a significant
> difference in your app's req/s, but it's a possibility depending on
> what your app is doing. Let us know the details of what you find

The results of the "ab" benchmark are for the same app with added
clear() to __before__ method and removed, everything else stays the
same.
model.session_context.current = make_session() has the same performace
as clear()
I'll try to figure out something.

--
Adam Hoscilo
http://hoscilo.pypla.net
http://blog.bootstrap.pl

Reply all
Reply to author
Forward
0 new messages