@classmethoddefconfigurable_base(cls):returnIOLoop@classmethoddefconfigurable_default(cls):ifhasattr(select,"epoll"):fromtornado.platform.epollimportEPollIOLoopreturnEPollIOLoopifhasattr(select,"kqueue"):#python 2.6+ on BSD or Macfromtornado.platform.kqueueimportKQueueIOLoopreturnKQueueIOLoopfromtornado.platform.selectimportSelectIOLoopreturnSelectIOLoop
defstart(self):ifnotlogging.getLogger().handlers:logging.basicConfigifself._stopped:self._stopped=Falsereturnold_current=getattr(IOLoop._current,"instance",None)IOLoop._current.instance=selfself._thread_ident=thread.get_ident()#Return the ‘thread identifier’ of the current thread. This is a nonzero integerself._running=Trueold_wakeup_fd=Noneifhasattr(singal,'set_wakeup_fd')andos.name=='posix':try:old_wakeup_fd=signal.set_wakeup_fd(self._waker.write_fileno())ifold_wakeup_fd!=-1:signal.set_walkeup_fd(old_wakeup_fd)old_walkup_fd=NoneexceptValueError:pass
上面这段代码 TBD
12345678910111213141516171819202122232425262728
whileTrue:poll_timeout=_POLL_TIMEOUT#Prevent IO event starvation by delaying new callbacks # to the next iteration of the event loop.withself._callback_lock:callbacks=self._callbacksself._callbacks=[]forcallbackincallbacks:self._run_callback(callback)# Closures may be holding on to a lot of memory, so allow# them to be freed before we go into our poll wait.callbacks=callback=Noneifself._timeouts:now=self.time()whileself_timeouts:ifself._timesouts[0].callbackisNone:# the timeout was cancelledheapq.heappop(self._timeouts)self._cancellations-=1elifself._timeouts[0].deadline<=now:timeout=heapq.heappop(self._timeouts)self._run_callback(timeout.callback)deltimeoutelse:seconds=self._timeouts[0].deadline-nowpoll_timeout=min(seconds,poll_timeout)break