[Python-checkins] cpython: Issue #20311: selectors: Add a resolution attribute to BaseSelector.

victor.stinner python-checkins at python.org
Sat Jan 25 15:02:54 CET 2014


http://hg.python.org/cpython/rev/3b8a2281d323
changeset: 88695:3b8a2281d323
user: Victor Stinner <victor.stinner at gmail.com>
date: Sat Jan 25 14:56:48 2014 +0100
summary:
 Issue #20311: selectors: Add a resolution attribute to BaseSelector.
files:
 Doc/library/selectors.rst | 4 ++++
 Lib/selectors.py | 23 ++++++++++++++++++++++-
 Lib/test/test_selectors.py | 5 +++++
 Misc/NEWS | 2 ++
 4 files changed, 33 insertions(+), 1 deletions(-)
diff --git a/Doc/library/selectors.rst b/Doc/library/selectors.rst
--- a/Doc/library/selectors.rst
+++ b/Doc/library/selectors.rst
@@ -98,6 +98,10 @@
 :class:`BaseSelector` and its concrete implementations support the
 :term:`context manager` protocol.
 
+ .. attribute:: resolution
+
+ Resolution of the selector in seconds.
+
 .. method:: register(fileobj, events, data=None)
 
 Register a file object for selection, monitoring it for I/O events.
diff --git a/Lib/selectors.py b/Lib/selectors.py
--- a/Lib/selectors.py
+++ b/Lib/selectors.py
@@ -5,7 +5,7 @@
 """
 
 
-from abc import ABCMeta, abstractmethod
+from abc import ABCMeta, abstractmethod, abstractproperty
 from collections import namedtuple, Mapping
 import functools
 import select
@@ -82,6 +82,11 @@
 performant implementation on the current platform.
 """
 
+ @abstractproperty
+ def resolution(self):
+ """Resolution of the selector in seconds"""
+ return None
+
 @abstractmethod
 def register(self, fileobj, events, data=None):
 """Register a file object.
@@ -283,6 +288,10 @@
 self._readers = set()
 self._writers = set()
 
+ @property
+ def resolution(self):
+ return 1e-6
+
 def register(self, fileobj, events, data=None):
 key = super().register(fileobj, events, data)
 if events & EVENT_READ:
@@ -335,6 +344,10 @@
 super().__init__()
 self._poll = select.poll()
 
+ @property
+ def resolution(self):
+ return 1e-3
+
 def register(self, fileobj, events, data=None):
 key = super().register(fileobj, events, data)
 poll_events = 0
@@ -385,6 +398,10 @@
 super().__init__()
 self._epoll = select.epoll()
 
+ @property
+ def resolution(self):
+ return 1e-3
+
 def fileno(self):
 return self._epoll.fileno()
 
@@ -445,6 +462,10 @@
 super().__init__()
 self._kqueue = select.kqueue()
 
+ @property
+ def resolution(self):
+ return 1e-9
+
 def fileno(self):
 return self._kqueue.fileno()
 
diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py
--- a/Lib/test/test_selectors.py
+++ b/Lib/test/test_selectors.py
@@ -363,6 +363,11 @@
 self.assertFalse(s.select(2))
 self.assertLess(time() - t, 2.5)
 
+ def test_resolution(self):
+ s = self.SELECTOR()
+ self.assertIsInstance(s.resolution, (int, float))
+ self.assertGreater(s.resolution, 0.0)
+
 
 class ScalableSelectorMixIn:
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@
 Library
 -------
 
+- Issue #20311: selectors: Add a resolution attribute to BaseSelector.
+
 - Issue #20189: unittest.mock now no longer assumes that any object for
 which it could get an inspect.Signature is a callable written in Python.
 Fix courtesy of Michael Foord.
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /