Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1611

Show
Ignore:
Timestamp:
02/05/07 13:12:08
Author:
fumanchu
Message:

Moved default threadlocal objects into the serving class instead of the proxy. Benchmark is now 6% faster.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r1610 r1611  
    159159from cherrypy import _cpdispatch as dispatch 
    160160from cherrypy import _cprequest 
     161from cherrypy.lib import http as _http 
    161162from cherrypy import _cpengine 
    162163engine = _cpengine.Engine() 
     
    191192# in a thread-safe way. 
    192193class _Serving(_local): 
    193     """An interface for registering request and response objects.""" 
     194    """An interface for registering request and response objects. 
     195     
     196    Rather than have a separate "thread local" object for the request and 
     197    the response, this class works as a single threadlocal container for 
     198    both objects. 
     199    """ 
     200     
     201    __metaclass__ = _AttributeDocstrings 
     202     
     203    request = _cprequest.Request(_http.Host("localhost", 80), 
     204                                 _http.Host("localhost", 1111)) 
     205    request__doc = """ 
     206    The request object for the current thread. In the main thread, 
     207    and any threads which are not receiving HTTP requests, this is None.""" 
     208     
     209    response = _cprequest.Response() 
     210    response__doc = """ 
     211    The response object for the current thread. In the main thread, 
     212    and any threads which are not receiving HTTP requests, this is None.""" 
    194213     
    195214    def load(self, request, response): 
     
    206225class _ThreadLocalProxy(object): 
    207226     
    208     __slots__ = ['__attrname__', '_default_child', '__dict__'] 
    209      
    210     def __init__(self, attrname, default): 
     227    __slots__ = ['__attrname__', '__dict__'] 
     228     
     229    def __init__(self, attrname): 
    211230        self.__attrname__ = attrname 
    212         self._default_child = default 
    213      
    214     def _get_child(self): 
    215         try: 
    216             return getattr(_serving, self.__attrname__) 
    217         except AttributeError: 
    218             # Bind dummy instances of default objects to help introspection. 
    219             return self._default_child 
    220231     
    221232    def __getattr__(self, name): 
    222         return getattr(self._get_child(), name) 
     233        child = getattr(serving, self.__attrname__) 
     234        return getattr(child, name) 
    223235     
    224236    def __setattr__(self, name, value): 
    225         if name in ("__attrname__", "_default_child"): 
     237        if name in ("__attrname__", ): 
    226238            object.__setattr__(self, name, value) 
    227239        else: 
    228             setattr(self._get_child(), name, value) 
     240            child = getattr(serving, self.__attrname__) 
     241            setattr(child, name, value) 
    229242     
    230243    def __delattr__(self, name): 
    231         delattr(self._get_child(), name) 
     244        child = getattr(serving, self.__attrname__) 
     245        delattr(child, name) 
    232246     
    233247    def _get_dict(self): 
    234         childobject = self._get_child(
    235         d = childobject.__class__.__dict__.copy() 
    236         d.update(childobject.__dict__) 
     248        child = getattr(serving, self.__attrname__
     249        d = child.__class__.__dict__.copy() 
     250        d.update(child.__dict__) 
    237251        return d 
    238252    __dict__ = property(_get_dict) 
    239253     
    240254    def __getitem__(self, key): 
    241         return self._get_child()[key] 
     255        child = getattr(serving, self.__attrname__) 
     256        return child[key] 
    242257     
    243258    def __setitem__(self, key, value): 
    244         self._get_child()[key] = value 
     259        child = getattr(serving, self.__attrname__) 
     260        child[key] = value 
    245261     
    246262    def __delitem__(self, key): 
    247         del self._get_child()[key] 
     263        child = getattr(serving, self.__attrname__) 
     264        del child[key] 
    248265     
    249266    def __contains__(self, key): 
    250         return key in self._get_child() 
     267        child = getattr(serving, self.__attrname__) 
     268        return key in child 
    251269 
    252270 
    253271# Create request and response object (the same objects will be used 
    254272#   throughout the entire life of the webserver, but will redirect 
    255 #   to the "_serving" object) 
    256 from cherrypy.lib import http as _http 
    257 request = _ThreadLocalProxy('request', 
    258                             _cprequest.Request(_http.Host("localhost", 80), 
    259                                                _http.Host("localhost", 1111))) 
    260 response = _ThreadLocalProxy('response', _cprequest.Response()) 
     273#   to the "serving" object) 
     274request = _ThreadLocalProxy('request') 
     275response = _ThreadLocalProxy('response') 
    261276 
    262277# Create thread_data object as a thread-specific all-purpose storage 
     
    271286    """Given an object or a path to an object, get the object and its name.""" 
    272287    if isinstance(thing, _ThreadLocalProxy): 
    273         thing = thing._get_child(
     288        thing = getattr(serving, thing.__attrname__
    274289    return pydoc._builtin_resolve(thing, forceload) 
    275290 
  • trunk/cherrypy/lib/sessions.py

    r1600 r1611  
    3737 
    3838class Session(object): 
    39     """A CherryPy dict-like Session object (one per request). 
    40      
    41     id: current session ID. 
    42     expiration_time (datetime): when the current session will expire. 
    43     timeout (minutes): used to calculate expiration_time from now. 
    44     clean_freq (minutes): the poll rate for expired session cleanup. 
    45     locked: If True, this session instance has exclusive read/write access 
    46         to session data. 
    47     loaded: If True, data has been retrieved from storage. This should 
    48         happen automatically on the first attempt to access session data. 
    49     """ 
     39    """A CherryPy dict-like Session object (one per request).""" 
     40     
     41    __metaclass__ = cherrypy._AttributeDocstrings 
     42     
     43    id = None 
     44    id__doc = "The current session ID." 
     45     
     46    timeout = 60 
     47    timeout__doc = "Number of minutes after which to delete session data." 
     48     
     49    locked = False 
     50    locked__doc = """ 
     51    If True, this session instance has exclusive read/write access 
     52    to session data.""" 
     53     
     54    loaded = False 
     55    loaded__doc = """ 
     56    If True, data has been retrieved from storage. This should happen 
     57    automatically on the first attempt to access session data.""" 
    5058     
    5159    clean_thread = None 
     60    clean_thread__doc = "Class-level PerpetualTimer which calls self.clean_up." 
     61     
     62    clean_freq = 5 
     63    clean_freq__doc = "The poll rate for expired session cleanup in minutes." 
    5264     
    5365    def __init__(self, id=None, **kwargs): 
    54         self.locked = False 
    55         self.loaded = False 
    5666        self._data = {} 
    5767         
     
    413423 
    414424 
    415 _def_session = RamSession() 
    416  
    417425def init(storage_type='ram', path=None, path_header=None, name='session_id', 
    418426         timeout=60, domain=None, secure=False, clean_freq=5, **kwargs): 
     
    440448     
    441449    # Guard against running twice 
    442     if hasattr(cherrypy._serving, "session"): 
     450    if hasattr(request, "_session_init_flag"): 
    443451        return 
     452    request._session_init_flag = True 
    444453     
    445454    # Check if request came with a session ID 
     
    457466     
    458467    if not hasattr(cherrypy, "session"): 
    459         cherrypy.session = cherrypy._ThreadLocalProxy('session', _def_session
     468        cherrypy.session = cherrypy._ThreadLocalProxy('session'
    460469        if hasattr(sess, "setup"): 
    461470            sess.setup() 

Hosted by WebFaction

Log in as guest/cpguest to create tickets