From: Ben Darnell Date: Wed, 3 Aug 2011 06:33:39 +0000 (-0700) Subject: Add install() method to IOLoop to allow a custom subclass as the singleton. X-Git-Tag: v2.1.0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ec7b47cf84bd6634f4899a75807ccb184e3d700;p=thirdparty%2Ftornado.git Add install() method to IOLoop to allow a custom subclass as the singleton. Singleton-related methods are now static, not class methods. --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 6198de32e..b50636f46 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -124,8 +124,8 @@ class IOLoop(object): lambda fd, events: self._waker.consume(), self.READ) - @classmethod - def instance(cls): + @staticmethod + def instance(): """Returns a global IOLoop instance. Most single-threaded applications have a single, global IOLoop. @@ -140,14 +140,24 @@ class IOLoop(object): def __init__(self, io_loop=None): self.io_loop = io_loop or IOLoop.instance() """ - if not hasattr(cls, "_instance"): - cls._instance = cls() - return cls._instance + if not hasattr(IOLoop, "_instance"): + IOLoop._instance = IOLoop() + return IOLoop._instance - @classmethod - def initialized(cls): + @staticmethod + def initialized(): """Returns true if the singleton instance has been created.""" - return hasattr(cls, "_instance") + return hasattr(IOLoop, "_instance") + + def install(self): + """Installs this IOloop object as the singleton instance. + + This is normally not necessary as `instance()` will create + an IOLoop on demand, but you may want to call `install` to use + a custom subclass of IOLoop. + """ + assert not IOLoop.initialized() + IOLoop._instance = self def close(self, all_fds=False): """Closes the IOLoop, freeing any resources used.