From: Evan Klitzke Date: Thu, 30 Jun 2011 00:50:13 +0000 (-0700) Subject: improve the speed of add_callback by avoiding the waker pipe when possible X-Git-Tag: v2.1.0~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ac8a059ef3bc9b15d1c28491c54b9f7ccb206c9;p=thirdparty%2Ftornado.git improve the speed of add_callback by avoiding the waker pipe when possible --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 0e6b8a766..45a849979 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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)))