cherrpy and threads

271 views
Skip to first unread message

spe...@gmail.com

unread,
Sep 2, 2007, 1:36:59 PM9/2/07
to cherrypy-users
Hello Everybody, I am having some trouble using cherrypy and threads
(not cherrypy threads, but custom ones).

Here's some code, http://pastebin.com/m75857566, and here's what I am
trying to accomplish:

I want to create a thread that does some stuff in the background,
while the user is displayed a "please wait" message.

Almost everything works fine, except one thing: I can't access the
initialized cherrpy instance I need in my "onComplete" function, more
specifically line 5 produces "AttributeError: '_Serving' object has no
attribute 'session' ".
I figured out that the "__init__" function has access to the
initialized cherrypy instance, while "run" does not.


As this is my first time working with threads, I would really
appreciate some input if what I am trying to do is making any sense at
all, and what I am doing wrong. Thanks.

Arnar Birgisson

unread,
Sep 2, 2007, 6:56:09 PM9/2/07
to cherryp...@googlegroups.com
Hi there,

On 9/2/07, spe...@gmail.com <spe...@gmail.com> wrote:
...


> Almost everything works fine, except one thing: I can't access the
> initialized cherrpy instance I need in my "onComplete" function, more
> specifically line 5 produces "AttributeError: '_Serving' object has no
> attribute 'session' ".
> I figured out that the "__init__" function has access to the
> initialized cherrypy instance, while "run" does not.

CherryPy relies heavily on so-called thread-locals. Those are
variables that have values within exactly just one thread. When
accessed from other threads, they have completely different values.
cherrypy.thread is one such variable.

I'm not exactly sure on this, but maybe if you assign cherrypy.session
to a local variable on your thread class from your main thread (before
starting the thread) - you can access it via that local variable from
inside your thread's run() method. Again, I'm not sure and I'm sure
Robert will kick my ass if I'm telling you nonsense.

The reason it's accessible from the __init__ is that __init__ is
really run in the thread you're instantiating the thread class from.
The second thread isn't started until you call .start() and then only
it's run() method is run in a seperate thread.

For info on thread locals, check out the class "local" on
http://docs.python.org/lib/module-threading.html.

Arnar

Robert Brewer

unread,
Sep 3, 2007, 12:19:16 AM9/3/07
to cherryp...@googlegroups.com

Arnar Birgisson wrote:
On 9/2/07, spe...@gmail.com <spe...@gmail.com> wrote:
...
> > Almost everything works fine, except one thing: I can't
> > access the initialized cherrpy instance I need in my
> > "onComplete" function, more specifically line 5 produces
> > "AttributeError: '_Serving' object has no attribute
> > 'session' ". I figured out that the "__init__" function
> > has access to the initialized cherrypy instance, while
> > "run" does not.
>
> CherryPy relies heavily on so-called thread-locals.
> Those are variables that have values within exactly
> just one thread. When accessed from other threads,
> they have completely different values. cherrypy.thread
> is one such variable.

Right.



> I'm not exactly sure on this, but maybe if you assign
> cherrypy.session to a local variable on your thread
> class from your main thread (before starting the thread)
> - you can access it via that local variable from inside
> your thread's run() method. Again, I'm not sure and I'm
> sure Robert will kick my ass if I'm telling you nonsense.

Well, that sounds like a good way to introduce memory leaks.
The threadlocal design is specifically designed to make it
easy to *not* persist any request-scoped data like the
session object. You'd probably be a lot safer storing just
the session variables you think you're going to need later.
But...that won't stick them back into the session. Hmmm...

Note also that cherrypy.session is not what you want anyway;
it's just a getattr proxy for cherrypy._serving.session,
which itself is just a front-end for the actual RAM or file
storage for sessions. So even if you could store the session
object, it's not going to do anything for you without re-
implementing all the other parts of the session library;
You're at least going to have to call session.init and
.save on your own, as well as handle locking. Sorry; the
session lib wasn't really designed to be accessed outside
of the request scope. :/


Robert Brewer
fuma...@aminus.org

Tim Roberts

unread,
Sep 4, 2007, 1:42:00 PM9/4/07
to cherryp...@googlegroups.com
spe...@gmail.com wrote:
> Hello Everybody, I am having some trouble using cherrypy and threads
> (not cherrypy threads, but custom ones).
>
> Here's some code, http://pastebin.com/m75857566, and here's what I am
> trying to accomplish:
>
> I want to create a thread that does some stuff in the background,
> while the user is displayed a "please wait" message.
>

How do you plan to do that? HTTP is not a real-time protocol. There's
no way to send part of a page, then pause, then send the rest of the
page, and have the user see it that way. The browser is allowed to wait
until the entire page arrives and the connection is closed before
displaying anything.

It's possible to do this with Javascript, or by using refreshes, but
multithreading doesn't help with that.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

spe...@gmail.com

unread,
Sep 4, 2007, 5:36:10 PM9/4/07
to cherrypy-users
Hi Guys,
thanks for replying. Looks like my question is not as trivial as I
first thought.

> How do you plan to do that?

Here's what I planned: Start a thread which does stuff in the
background, and then show the user a "please wait" page which
automatically fetches the status of the thread via ajax. Once that
thread is finished, I would like to write the result to a session and
display it.

I researched this group regarding "please wait"-pages and found
suggestions for threads, which was comprehensible for the obvious
reasons. The problem seems to be (as elaborated by Robert and Arnar)
accessing the session attribute from the second thread.

If you have created something similar or know of a workaround for my
issue, I would appreciate hearing from you. Thanks!


On Sep 4, 7:42 pm, Tim Roberts <t...@probo.com> wrote:


> spei...@gmail.com wrote:
> > Hello Everybody, I am having some trouble using cherrypy and threads
> > (not cherrypy threads, but custom ones).
>

> > Here's some code,http://pastebin.com/m75857566, and here's what I am


> > trying to accomplish:
>
> > I want to create a thread that does some stuff in the background,
> > while the user is displayed a "please wait" message.
>
> How do you plan to do that? HTTP is not a real-time protocol. There's
> no way to send part of a page, then pause, then send the rest of the
> page, and have the user see it that way. The browser is allowed to wait
> until the entire page arrives and the connection is closed before
> displaying anything.
>
> It's possible to do this with Javascript, or by using refreshes, but
> multithreading doesn't help with that.
>
> --

> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.

Reply all
Reply to author
Forward
0 new messages