[Pkg-bitcoin-commits] [python-quamash] 31/269: Start support for Unix

Jonas Smedegaard dr at jones.dk
Fri Nov 24 11:26:13 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository python-quamash.

commit 52507b55a879b3032298150013a336fe48bf7ed0
Author: Arve Knudsen <arve.knudsen at gmail.com>
Date:   Tue Jul 1 20:16:35 2014 +0200

    Start support for Unix
---
 quamash/__init__.py | 100 +++++++++-------------------------------------------
 quamash/_common.py  |  12 +++++++
 quamash/_unix.py    |   6 ++++
 quamash/_windows.py |  65 ++++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 83 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index 54bf050..d1ffd44 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -26,25 +26,16 @@ except ImportError:
     from PyQt5 import QtCore
     QtCore.Signal = QtCore.pyqtSignal
 
-_logger = logging.getLogger(__name__)
-
+from ._common import with_logger
 
-def _with_logger(cls):
-    """Class decorator to add a logger to a class."""
-    attr_name = '_logger'
-    cls_name = cls.__qualname__
-    module = cls.__module__
-    if module is not None:
-        cls_name = module + '.' + cls_name
-    setattr(cls, attr_name, logging.getLogger(cls_name))
-    return cls
+_logger = logging.getLogger(__name__)
 
 
- at _with_logger
+ at with_logger
 class _EventPoller(QtCore.QObject):
     """Polling of events in separate thread."""
     sig_events = QtCore.Signal(list)
-    
+
     def __init__(self, selector):
         super(_EventPoller, self).__init__()
         self.__semaphore = threading.Semaphore(0)
@@ -67,14 +58,16 @@ class _EventPoller(QtCore.QObject):
         self.__semaphore.release()
 
         while not self.__canceled:
+            self._logger.info('Polling for events')
             events = self.__selector.select(0.01)
+            self._logger.info('Got events {}'.format(events))
             if events:
                 self.sig_events.emit(events)
 
         self.__semaphore.release()
 
 
- at _with_logger
+ at with_logger
 class _QThreadWorker(QtCore.QThread):
     """
     Read from the queue.
@@ -107,7 +100,7 @@ class _QThreadWorker(QtCore.QThread):
         self.__stop = True
 
 
- at _with_logger
+ at with_logger
 class QThreadExecutor(QtCore.QObject):
     """
     ThreadExecutor that produces QThreads
@@ -187,74 +180,16 @@ def _easycallback(fn):
 
 
 if os.name == 'nt':
-    import _winapi
-    from asyncio import windows_events
-    _baseclass = asyncio.ProactorEventLoop
-
-    @_with_logger
-    class _IocpProactor(windows_events.IocpProactor):
-        def __init__(self):
-            self.__events = []
-            super(_IocpProactor, self).__init__()
-
-        def select(self, timeout=None):
-            """Override in order to handle events in a threadsafe manner."""
-            if not self.__events:
-                self._poll(timeout)
-            tmp = self.__events
-            self.__events = []
-            return tmp
-
-        def close(self):
-            self._logger.debug('Closing')
-            super(_IocpProactor, self).close()
-
-        def _poll(self, timeout=None):
-            """Override in order to handle events in a threadsafe manner."""
-            import math
-            from asyncio import _overlapped
-            INFINITE = 0xffffffff
-
-            if timeout is None:
-                ms = INFINITE
-            elif timeout < 0:
-                raise ValueError("negative timeout")
-            else:
-                # GetQueuedCompletionStatus() has a resolution of 1 millisecond,
-                # round away from zero to wait *at least* timeout seconds.
-                ms = math.ceil(timeout * 1e3)
-                if ms >= INFINITE:
-                    raise ValueError("timeout too big")
-
-            while True:
-                self._logger.debug('Polling IOCP with timeout {} ms in thread {}...'.format(
-                    ms, threading.get_ident()))
-                status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
-
-                if status is None:
-                    break
-                err, transferred, key, address = status
-                try:
-                    f, ov, obj, callback = self._cache.pop(address)
-                except KeyError:
-                    # key is either zero, or it is used to return a pipe
-                    # handle which should be closed to avoid a leak.
-                    if key not in (0, _overlapped.INVALID_HANDLE_VALUE):
-                        _winapi.CloseHandle(key)
-                    ms = 0
-                    continue
-
-                if obj in self._stopped_serving:
-                    f.cancel()
-                elif not f.cancelled():
-                    self.__events.append((f, callback, transferred, key, ov))
-
-                ms = 0
+    from . import _windows
+    _baseclass = _windows.baseclass
+    _selector_cls = _windows.selector_cls
 else:
-    _baseclass = asyncio.SelectorEventLoop
+    from . import _unix
+    _baseclass = _unix.baseclass
+    _selector_cls = _unix.selector_cls
 
 
- at _with_logger
+ at with_logger
 class QEventLoop(QtCore.QObject, _baseclass):
     """
     Implementation of asyncio event loop that uses the Qt Event loop
@@ -277,8 +212,7 @@ class QEventLoop(QtCore.QObject, _baseclass):
         self.__exception_handler = None
 
         super(QEventLoop, self).__init__()
-        baseclass_args = (_IocpProactor(),) if os.name == 'nt' else ()
-        _baseclass.__init__(self, *baseclass_args)
+        _baseclass.__init__(self, _selector_cls())
 
         self.__event_poller = _EventPoller(self._selector)
         self.__event_poller.sig_events.connect(self.__on_events)
@@ -570,4 +504,4 @@ class _Cancellable:
 
     def cancel(self):
         self.__loop.remove(timer)
-        self.__timer.stop()
\ No newline at end of file
+        self.__timer.stop()
diff --git a/quamash/_common.py b/quamash/_common.py
new file mode 100644
index 0000000..bc44f31
--- /dev/null
+++ b/quamash/_common.py
@@ -0,0 +1,12 @@
+import logging
+
+
+def with_logger(cls):
+    """Class decorator to add a logger to a class."""
+    attr_name = '_logger'
+    cls_name = cls.__qualname__
+    module = cls.__module__
+    if module is not None:
+        cls_name = module + '.' + cls_name
+    setattr(cls, attr_name, logging.getLogger(cls_name))
+    return cls
diff --git a/quamash/_unix.py b/quamash/_unix.py
new file mode 100644
index 0000000..139def0
--- /dev/null
+++ b/quamash/_unix.py
@@ -0,0 +1,6 @@
+import asyncio
+from asyncio import selectors
+
+baseclass = asyncio.SelectorEventLoop
+
+selector_cls = selectors.DefaultSelector
diff --git a/quamash/_windows.py b/quamash/_windows.py
new file mode 100644
index 0000000..7528918
--- /dev/null
+++ b/quamash/_windows.py
@@ -0,0 +1,65 @@
+import _winapi
+import asyncio
+from asyncio import windows_events
+
+baseclass = asyncio.ProactorEventLoop
+
+ at with_logger
+class IocpProactor(windows_events.IocpProactor):
+    def __init__(self):
+        self.__events = []
+        super(IocpProactor, self).__init__()
+
+    def select(self, timeout=None):
+        """Override in order to handle events in a threadsafe manner."""
+        if not self.__events:
+            self._poll(timeout)
+        tmp = self.__events
+        self.__events = []
+        return tmp
+
+    def close(self):
+        self._logger.debug('Closing')
+        super(IocpProactor, self).close()
+
+    def _poll(self, timeout=None):
+        """Override in order to handle events in a threadsafe manner."""
+        import math
+        from asyncio import _overlapped
+        INFINITE = 0xffffffff
+
+        if timeout is None:
+            ms = INFINITE
+        elif timeout < 0:
+            raise ValueError("negative timeout")
+        else:
+            # GetQueuedCompletionStatus() has a resolution of 1 millisecond,
+            # round away from zero to wait *at least* timeout seconds.
+            ms = math.ceil(timeout * 1e3)
+            if ms >= INFINITE:
+                raise ValueError("timeout too big")
+
+        while True:
+            self._logger.debug('Polling IOCP with timeout {} ms in thread {}...'.format(
+                ms, threading.get_ident()))
+            status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
+
+            if status is None:
+                break
+            err, transferred, key, address = status
+            try:
+                f, ov, obj, callback = self._cache.pop(address)
+            except KeyError:
+                # key is either zero, or it is used to return a pipe
+                # handle which should be closed to avoid a leak.
+                if key not in (0, _overlapped.INVALID_HANDLE_VALUE):
+                    _winapi.CloseHandle(key)
+                ms = 0
+                continue
+
+            if obj in self._stopped_serving:
+                f.cancel()
+            elif not f.cancelled():
+                self.__events.append((f, callback, transferred, key, ov))
+
+            ms = 0

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/python-quamash.git



More information about the Pkg-bitcoin-commits mailing list