On 1/4/07, Arnar Birgisson <[EMAIL PROTECTED]> wrote:
On 1/4/07, percious <[EMAIL PROTECTED]> wrote:
> My problem is that I am trying to store the progress in a session
> variable so each user has their own particular progress. Later on I
> will probably expand this in case the same user has multiple progress
> bars...
>
> The problem I am having is that cherrypy does not see the updates to
> the 'progress' variable in the session. Here is my sample controller:
I'm certainly not an expert on this, but I beleive that CherryPy
stores the session for each request in a thread-local dictionary, so
probably - inside the workder thread cherrypy.session is something
different.
You could keep a global dict, and in progressPage - select some
identifier that doesn't appear in the dict and pass that down to the
template. Then make the js in the template send this id when it
requests getProgress - which in turn looks that up in the dict. You
would have to find some suitable place to clear out the value too.
This would also allow you to define many simultaneous progress reports
for a session. However, I'm just improvising now :o)
I read my message again and found myself sounding a bit unclear, so
here's the code:
import cherrypy
import thread
class MyController:
def __init__(self):
self._progressdict = dict()
@expose(format='json')
def getProgress(self):
progress = self._progressdict.get(int(progress_id), 100)
return dict(progress=progress)
def _doSomething(self, arg, progress_id):
progressIncrement = 100/arg
for i in range(arg):
time.sleep(1)
self._progressdict[progress_id] += progressIncrement
del self._progressdict[progress_id]
@expose()
def progressPage(self):
progress_id = max(self._progressdict.keys())+1
self._progressdict[progress_id] = 0
thread.start_new_thread(self._doSomething, (10,), progress_id)
return dict(progress_id=progress_id)
The access of the dict progress_id is not thread safe here though, but
that's easy to fix. Especially the first two lines in progressPage
need to be atomic.
Arnar
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---