2011/2/15 Ben Hoyt <[email protected]>: > Thanks, Anand. >> >> In most wsgi adapters, threads are recycled by using a thread pool. So >> it is important to clear all the threadeddict objects used in the >> system to avoid interference with later requests. currently, it is >> done like this: >> >> https://github.com/webpy/webpy/blob/master/web/application.py#L109 >> >> I think this can be solved by maintaing the list of instances and >> providing a clear_all method to clear all instances. > > Very good point -- I'd missed that. How does this look? > class ThreadedDict(threading.local): > _instances = [] > @classmethod > def clear_all(cls): > for d in cls._instances: > d.clear() > def __init__(self): > self._instances.append(self) > # ... other methods I gave earlier ... > And then in application._cleanup() > def _cleanup(self): > utils.ThreadedDict.clear_all() > > (IMHO _cleanup should be calling a ThreadedDict class function anyway, > rather than messing with the _d internals of ThreadedDict.)
Agreed. I've already implemented it. I've also implemented a threadlocal class to fall back when running on Python 2.3. >> And the Session class is extended from ThreadedDict and it is using >> __dict__ for storing some private data. This will fail because of >> that. >> >> https://github.com/webpy/webpy/blob/master/web/session.py#L45 >> >> I'm looking in to these issues right now. Will report back once I have >> something ready. > > We're not using web.py's session module, but I take it this won't work > because __dict__ as used here means those items *aren't* thread-local? You > can actually achieve a similar thing by using __slots__ in a threading.local > subclass. From Python's standard library _threading_local.py examples: > Note that subclasses can define slots, but they are not thread > local. They are shared across threads: > >>> class MyLocal(local): > ... __slots__ = 'number' > >>> mydata = MyLocal() > >>> mydata.number = 42 > >>> mydata.color = 'red' > So, the separate thread: > >>> thread = threading.Thread(target=f) > >>> thread.start() > >>> thread.join() > affects what we see: > >>> mydata.number > 11 Ah! I didn't know that. Thanks for the suggestion. I'll implement that and push it tomorrow. Anand -- You received this message because you are subscribed to the Google Groups "web.py" 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/webpy?hl=en.
