From 6ec7b47cf84bd6634f4899a75807ccb184e3d700 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Tue, 2 Aug 2011 23:33:39 -0700 Subject: [PATCH] Add install() method to IOLoop to allow a custom subclass as the singleton. Singleton-related methods are now static, not class methods. --- tornado/ioloop.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) 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. -- 2.47.2