Hang on simultaneous requests

View: New views
7 Messages — Rating Filter:   Alert me  

Hang on simultaneous requests

by demitri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

I'm having a problem with my 2.0.3 installation. Let's say I click on
a link that results in a long request to my database. While waiting, I
click on the same link (or another link) that similarly generates a
request to the database, the whole app then hangs.

All of my database access is defined in a custom module that I use
with other (non TG) scripts, so I've disabled all of the built-in
database connectivity (or at least don't import it).

This is how I generate the Session class:

Session = scoped_session(sessionmaker(bind=engine, autocommit=True,
autoflush=False))

I create an instance of Session in the file controllers/__init__.py:

session = Session()

and then use that as needed in each controller.

Is this correct, or is there a better practice?

Cheers,
Demitri


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Hang on simultaneous requests

by Diez B. Roggisch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


demitri schrieb:

> Hi,
>
> I'm having a problem with my 2.0.3 installation. Let's say I click on
> a link that results in a long request to my database. While waiting, I
> click on the same link (or another link) that similarly generates a
> request to the database, the whole app then hangs.
>
> All of my database access is defined in a custom module that I use
> with other (non TG) scripts, so I've disabled all of the built-in
> database connectivity (or at least don't import it).
>
> This is how I generate the Session class:
>
> Session = scoped_session(sessionmaker(bind=engine, autocommit=True,
> autoflush=False))
>
> I create an instance of Session in the file controllers/__init__.py:
>
> session = Session()

This is wrong. You don't instantiate the session on that level. You
simply use the generated class. One can instantiate it per request - but
that's not even needed, the class proxies calls to it to a per-thread
instance.

Diez

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Hang on simultaneous requests

by demitri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Diez,

Thanks for the reply. I'm still not sure about how this should work.
The result of scoped_session is a class, not an instantiated session.
The documentation says I should take the result of that and create a
session object, e.g.

Session = scoped_session(sessionmaker(...))

session = Session()

If I do as you suggest, e.g. Session.query(...), I get this error:

TypeError: unbound method query() must be called with Session instance
as first argument (got DeclarativeMeta instance instead)

which doesn't surprise me. But when I create a brand new project via
paster, nowhere is Session() being called?

What am I missing??

Thanks for the help,
Demitri

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Hang on simultaneous requests

by Diez B. Roggisch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


demitri schrieb:

> Hi Diez,
>
> Thanks for the reply. I'm still not sure about how this should work.
> The result of scoped_session is a class, not an instantiated session.
> The documentation says I should take the result of that and create a
> session object, e.g.
>
> Session = scoped_session(sessionmaker(...))
>
> session = Session()
>
> If I do as you suggest, e.g. Session.query(...), I get this error:
>
> TypeError: unbound method query() must be called with Session instance
> as first argument (got DeclarativeMeta instance instead)
>
> which doesn't surprise me. But when I create a brand new project via
> paster, nowhere is Session() being called?
>
> What am I missing??

This is strange. We do this:

DBSession = scoped_session(sessionmaker(
     autoflush=True,
     autocommit=False,
     ))

And we use it directly as this:

     def test_documents(self):
         immutable = db.Immutable(uid=random_uid(), mime_type="LiveSet",
size=100000)
         imm_child = db.Immutable(uid=random_uid(), mime_type="FLAC",
size=100000)
         immutable.children.append(imm_child)
         DBSession.flush() #@UndefinedVariable



I can't tell you why the direct use doesn't work. But what for sure is
wrong is to instantiate the session in __init__ - because that creates
*one session to rule them all* - which won't work. You don't want only
one global transaction for the whole lifetime of your program...

Try then instantiating the session on demand - e.g. in controllers.

Diez

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Hang on simultaneous requests

by demitri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Diez,

Thanks for the info. I've tried to instantiate sessions in each def:
where one is used, but that's lead to another problem:

TimeoutError: QueuePool limit of size 5 overflow 10 reached,
connection timed out, timeout 30

I'm guessing I need to do a session.close() at the end of each request/
response, but I don't know where that would bee (or even where to look
- this is actually all really frustrating).

This may or may not be related, but is it better to use
autocommit=True or False in sessions?

Cheers,
Demitri
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Hang on simultaneous requests

by demitri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

I may have got it. The thread entitled "Usage of sqlalchemy session
objects in turbogears" was quite helpful and exactly what I'm trying
to do. For others, this is what I have done. I have an existing
SQLAlchemy class that I use outside of TG (and need to keep it that
way). I removed any import of the built in model, which meant I needed
to implement my own session. What I did above was ok:

Session = scoped_session(sessionmaker(...))

Then I import it as such in the file controllers/__init__.py so it's
available to all controllers:

from mySAwrapper import Session as session

Then I can perform queries as expected on demand - no other setup/
cleaning.

session.query(...)

The scoped_session is the bit that instantiates sessions as needed.

I'm still not 100% sure all this is right, but I think I'm a lot
closer.

Cheers,
Demitri
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Re: Hang on simultaneous requests

by Mark Ramm :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> The scoped_session is the bit that instantiates sessions as needed.
>
> I'm still not 100% sure all this is right, but I think I'm a lot
> closer.

That should be fine, and IIRC it's close to exactly what I did on a
very old TG app that I did 2 jobs ago.

--

You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@....
For more options, visit this group at http://groups.google.com/group/turbogears?hl=.