List Info

Thread: cherrpy and threads




cherrpy and threads
country flaguser name
United States
2007-09-02 12:36:59
Hello Everybody, I am having some trouble using cherrypy and
threads
(not cherrypy threads, but custom ones).

Here's some code, http://pastebin.com/m75
857566, 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.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: cherrpy and threads
user name
2007-09-02 17:56:09
Hi there,

On 9/2/07, speigeigmail.com <speigeigmail.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
htt
p://docs.python.org/lib/module-threading.html.

Arnar

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: cherrpy and threads
country flaguser name
United States
2007-09-02 23:19:16

Arnar Birgisson wrote:
On 9/2/07, speigeigmail.com <speigeigmail.com&gt; wrote:
...
> > Almost everything works fine, except one thing: I can't
&gt; > 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
&gt; > "run&quot; 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
&gt; cherrypy.session to a local variable on your thread
&gt; class from your main thread (before starting the thread)
> - you can access it via that local variable from inside
&gt; 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
fumanchuaminus.org


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users&quot; group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Re: cherrpy and threads
country flaguser name
United States
2007-09-04 12:42:00
speigeigmail.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/m75
857566, 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, timrprobo.com
Providenza & Boekelheide, Inc.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: cherrpy and threads
country flaguser name
United States
2007-09-04 16:36:10
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/m75
857566, 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.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )