[Pkg-bitcoin-commits] [python-quamash] 263/269: replace _easycallback with _make_signaller

Jonas Smedegaard dr at jones.dk
Fri Nov 24 11:26:42 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 bbab9e30e10b71a95687b03a93524173fb7b43f0
Author: Mark Harviston <mark.harviston at gmail.com>
Date:   Sun Jan 18 21:16:43 2015 -0800

    replace _easycallback with _make_signaller
    
    _easycallback is hard to use indirectly to create signals at runtime,
    _make_signaller is more flexible & designed specifically to creat
    signals at runtime.
    
    _windows used it's own signal creation mechanism, and now it uses the
    same one as _call_soon_threadsafe.
---
 quamash/__init__.py | 69 ++++++++++-------------------------------------------
 quamash/_windows.py | 13 ++++++----
 2 files changed, 20 insertions(+), 62 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index ee8e45c..c0f9b4f 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -12,7 +12,6 @@ import sys
 import os
 import asyncio
 import time
-from functools import wraps
 import itertools
 from queue import Queue
 from concurrent.futures import Future
@@ -48,9 +47,6 @@ if QtModuleName == 'PyQt5':
 else:
 	QApplication = QtGui.QApplication
 
-if not hasattr(QtCore, 'Signal'):
-	QtCore.Signal = QtCore.pyqtSignal
-
 
 from ._common import with_logger
 
@@ -165,57 +161,13 @@ class QThreadExecutor:
 		self.shutdown()
 
 
-def _easycallback(fn):
-	"""
-	Decorator that wraps a callback in a signal.
-
-	It also packs & unpacks arguments, and makes the wrapped function effectively
-	threadsafe. If you call the function from one thread, it will be executed in
-	the thread the QObject has affinity with.
-
-	Remember: only objects that inherit from QObject can support signals/slots
-
-	>>> import asyncio
-	>>>
-	>>> import quamash
-	>>> QThread, QObject = quamash.QtCore.QThread, quamash.QtCore.QObject
-	>>>
-	>>> app = getfixture('application')
-	>>>
-	>>> global_thread = QThread.currentThread()
-	>>> class MyObject(QObject):
-	...     @_easycallback
-	...     def mycallback(self):
-	...         global global_thread, mythread
-	...         cur_thread = QThread.currentThread()
-	...         assert cur_thread is not global_thread
-	...         assert cur_thread is mythread
-	>>>
-	>>> mythread = QThread()
-	>>> mythread.start()
-	>>> myobject = MyObject()
-	>>> myobject.moveToThread(mythread)
-	>>>
-	>>> @asyncio.coroutine
-	... def mycoroutine():
-	...     myobject.mycallback()
-	>>>
-	>>> loop = QEventLoop(app)
-	>>> asyncio.set_event_loop(loop)
-	>>> with loop:
-	...     loop.run_until_complete(mycoroutine())
-	"""
-	@wraps(fn)
-	def in_wrapper(self, *args, **kwargs):
-		return signaler.signal.emit(self, args, kwargs)
-
-	class Signaler(QtCore.QObject):
-		signal = QtCore.Signal(object, tuple, dict)
-
-	signaler = Signaler()
-	signaler.signal.connect(lambda self, args, kwargs: fn(self, *args, **kwargs))
-	return in_wrapper
-
+def _make_signaller(qtimpl_qtcore, *args):
+	class Signaller(qtimpl_qtcore.QObject):
+		try:
+			signal = qtimpl_qtcore.Signal(*args)
+		except AttributeError:
+			signal = qtimpl_qtcore.pyqtSignal(*args)
+	return Signaller()
 
 if os.name == 'nt':
 	from . import _windows
@@ -258,6 +210,10 @@ class QEventLoop(_baseclass):
 		self._read_notifiers = {}
 		self._write_notifiers = {}
 
+		self.__call_soon_signaller = signaller = _make_signaller(QtCore, object, tuple)
+		self.__call_soon_signal = signaller.signal
+		signaller.signal.connect(lambda callback, args: self.call_soon(callback, *args))
+
 		assert self.__app is not None
 
 		super().__init__()
@@ -491,10 +447,9 @@ class QEventLoop(_baseclass):
 
 	# Methods for interacting with threads.
 
-	@_easycallback
 	def call_soon_threadsafe(self, callback, *args):
 		"""Thread-safe version of call_soon."""
-		self.call_soon(callback, *args)
+		self.__call_soon_signal.emit(callback, args)
 
 	def run_in_executor(self, executor, callback, *args):
 		"""Run callback in executor.
diff --git a/quamash/_windows.py b/quamash/_windows.py
index f442a63..fb586ef 100644
--- a/quamash/_windows.py
+++ b/quamash/_windows.py
@@ -15,7 +15,7 @@ except ImportError:  # noqa
 
 import math
 
-from . import QtCore
+from . import QtCore, _make_signaller
 from ._common import with_logger
 
 UINT32_MAX = 0xffffffff
@@ -28,8 +28,10 @@ class _ProactorEventLoop(asyncio.ProactorEventLoop):
 	def __init__(self):
 		super().__init__(_IocpProactor())
 
-		self.__event_poller = _EventPoller()
-		self.__event_poller.sig_events.connect(self._process_events)
+		self.__event_signaller = _make_signaller(QtCore, list)
+		self.__event_signal = self.__event_signaller.signal
+		self.__event_signal.connect(self._process_events)
+		self.__event_poller = _EventPoller(self.__event_signal)
 
 	def _process_events(self, events):
 		"""Process events from proactor."""
@@ -166,11 +168,12 @@ class _EventWorker(QtCore.QThread):
 
 
 @with_logger
-class _EventPoller(QtCore.QObject):
+class _EventPoller:
 
 	"""Polling of events in separate thread."""
 
-	sig_events = QtCore.Signal(list)
+	def __init__(self, sig_events):
+		self.sig_events = sig_events
 
 	def start(self, proactor):
 		self._logger.debug('Starting (proactor: {})...'.format(proactor))

-- 
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