Merge pull request #497 from polymorphm/master

tornado.ioloop.IOLoop.instance() should be threadsafe
This commit is contained in:
bdarnell 2012-05-06 17:41:29 -07:00
commit 8f44acda38
1 changed files with 7 additions and 1 deletions

View File

@ -104,6 +104,9 @@ class IOLoop(object):
WRITE = _EPOLLOUT
ERROR = _EPOLLERR | _EPOLLHUP
# Global lock for creating global IOLoop instance
_instance_lock = threading.Lock()
def __init__(self, impl=None):
self._impl = impl or _poll()
if hasattr(self._impl, 'fileno'):
@ -142,7 +145,10 @@ class IOLoop(object):
self.io_loop = io_loop or IOLoop.instance()
"""
if not hasattr(IOLoop, "_instance"):
IOLoop._instance = IOLoop()
with IOLoop._instance_lock:
if not hasattr(IOLoop, "_instance"):
# New instance after double check
IOLoop._instance = IOLoop()
return IOLoop._instance
@staticmethod