]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
improve the speed of add_callback by avoiding the waker pipe when possible
authorEvan Klitzke <evan@eklitzke.org>
Thu, 30 Jun 2011 00:50:13 +0000 (17:50 -0700)
committerEvan Klitzke <evan@eklitzke.org>
Thu, 30 Jun 2011 00:50:13 +0000 (17:50 -0700)
tornado/ioloop.py

index 0e6b8a7661ca4fa51797e445d21ab2969b459335..45a8499791a0e7287ab4d28676a3a448e07bd380 100644 (file)
@@ -31,6 +31,7 @@ import heapq
 import os
 import logging
 import select
+import thread
 import time
 import traceback
 
@@ -116,6 +117,7 @@ class IOLoop(object):
         self._timeouts = []
         self._running = False
         self._stopped = False
+        self._thread_ident = None
         self._blocking_signal_threshold = None
 
         # Create a pipe that we send bogus data to when we want to wake
@@ -160,7 +162,7 @@ class IOLoop(object):
 
     def close(self, all_fds=False):
         """Closes the IOLoop, freeing any resources used.
-        
+
         If ``all_fds`` is true, all file descriptors registered on the
         IOLoop will be closed (not just the ones created by the IOLoop itself.
         """
@@ -242,6 +244,7 @@ class IOLoop(object):
         if self._stopped:
             self._stopped = False
             return
+        self._thread_ident = thread.get_ident()
         self._running = True
         while True:
             # Never use an infinite timeout here - it can stall epoll
@@ -360,7 +363,7 @@ class IOLoop(object):
         The argument is a handle as returned by add_timeout.
         """
         # Removing from a heap is complicated, so just leave the defunct
-        # timeout object in the queue (see discussion in 
+        # timeout object in the queue (see discussion in
         # http://docs.python.org/library/heapq.html).
         # If this turns out to be a problem, we could add a garbage
         # collection pass whenever there are too many dead timeouts.
@@ -375,7 +378,7 @@ class IOLoop(object):
         from that IOLoop's thread.  add_callback() may be used to transfer
         control from other threads to the IOLoop's thread.
         """
-        if not self._callbacks:
+        if not self._callbacks and thread.get_ident() != self._thread_ident:
             self._wake()
         self._callbacks.append(stack_context.wrap(callback))
 
@@ -433,7 +436,7 @@ class _Timeout(object):
     # Comparison methods to sort by deadline, with object id as a tiebreaker
     # to guarantee a consistent ordering.  The heapq module uses __le__
     # in python2.5, and __lt__ in 2.6+ (sort() and most other comparisons
-    # use __lt__).  
+    # use __lt__).
     def __lt__(self, other):
         return ((self.deadline, id(self)) <
                 (other.deadline, id(other)))