[Pkg-bitcoin-commits] [python-quamash] 07/78: Testing better performances

Jonas Smedegaard dr at jones.dk
Tue Dec 19 01:19:51 UTC 2017


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

js pushed a commit to annotated tag debian/0.6.0_dfsg-1
in repository python-quamash.

commit 89bb10acba6c52a40cdc433c1402ef68e6d74364
Author: inso <insomniak.fr at gmaiL.com>
Date:   Sat Sep 10 12:52:57 2016 +0200

    Testing better performances
---
 quamash/__init__.py      | 81 ++++++++++++++++++++++++------------------------
 tests/test_qeventloop.py | 23 +++++++-------
 2 files changed, 53 insertions(+), 51 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index fdfa5f2..d2fc3ad 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -170,27 +170,47 @@ def _make_signaller(qtimpl_qtcore, *args):
 	return Signaller()
 
 
+ at with_logger
 class _SimpleTimer(QtCore.QObject):
-	def __init__(self, timeout, callback):
+	#__slots__ = 'callback', 'timer_id', 'stopped'
+	def __init__(self):
 		super().__init__()
-		self.callback = callback
-		self.timer_id = self.startTimer(timeout)
-		self.stopped = False
+		self.__callbacks = {}
+		self._stopped = False
+
+	def add_callback(self, handle, delay=0):
+		#self._logger.debug('Adding callback {} with delay {}'.format(handle, delay))
+		#timer = _SimpleTimer(delay * 1000, upon_timeout)
+		#self.__timers.append(timer)
+		timerid = self.startTimer(delay * 1000)
+		self._logger.debug("Registering timer id {0}".format(timerid))
+		assert timerid not in self.__callbacks
+		self.__callbacks[timerid] = handle
+		return handle
 
 	def timerEvent(self, event):  # noqa
-		assert self.timer_id == event.timerId()
-		if self.stopped:
-			self.killTimer(self.timer_id)
-		elif event.timerId() == self.timer_id:
-			self.callback()
-			self.killTimer(self.timer_id)
-			self.stopped = True
+		timerid = event.timerId()
+		self._logger.debug("Timer event on id {0}".format(timerid))
+		if self._stopped:
+			self._logger.debug("Timer stopped, killing {}".format(timerid))
+			self.__callbacks.pop(timerid)
+			self.killTimer(timerid)
+		else:
+			try:
+				handle = self.__callbacks.pop(timerid)
+			except KeyError as e :
+				self._logger.debug(str(e))
+				pass
+			if handle._cancelled:
+				self._logger.debug("Handle {} cancelled".format(handle))
+			else:
+				self._logger.debug("Calling handle {}".format(handle))
+				handle._run()
+			self.killTimer(timerid)
 
 	def stop(self):
-		self.stopped = True
-
-	def cancel(self):
-		self.stopped = True
+		self._logger.debug("Stopping timers")
+		self._stopped = True
 
 
 @with_logger
@@ -216,7 +236,6 @@ class _QEventLoop:
 	"""
 
 	def __init__(self, app=None):
-		self.__timers = []
 		self.__app = app or QApplication.instance()
 		assert self.__app is not None, 'No QApplication has been instantiated'
 		self.__is_running = False
@@ -225,6 +244,7 @@ class _QEventLoop:
 		self.__exception_handler = None
 		self._read_notifiers = {}
 		self._write_notifiers = {}
+		self._timer = _SimpleTimer()
 
 		self.__call_soon_signaller = signaller = _make_signaller(QtCore, object, tuple)
 		self.__call_soon_signal = signaller.signal
@@ -238,6 +258,7 @@ class _QEventLoop:
 		"""Run eventloop forever."""
 		self.__is_running = True
 		self._before_run_forever()
+
 		try:
 			self._logger.debug('Starting Qt event loop')
 			rslt = self.__app.exec_()
@@ -296,10 +317,9 @@ class _QEventLoop:
 
 		super().close()
 
-		for timer in self.__timers:
-			timer.stop()
+		self._timer.stop()
 
-		self.__timers = None
+		self._timer = None
 
 		self.__app = None
 
@@ -319,26 +339,7 @@ class _QEventLoop:
 		self._logger.debug(
 			'Registering callback {} to be invoked with arguments {} after {} second(s)'
 			.format(callback, args, delay))
-		return self._add_callback(asyncio.Handle(callback, args, self), delay)
-
-	def _add_callback(self, handle, delay=0):
-		def upon_timeout():
-			nonlocal timer
-			nonlocal handle
-			if timer in self.__timers:
-				self.__timers.remove(timer)
-			else:
-				self._logger.warn('Timer {} not among {}'.format(timer, self.__timers))
-			timer = None
-			self._logger.debug('Callback timer fired, calling {}'.format(handle))
-			handle._run()
-			handle = None
-
-		self._logger.debug('Adding callback {} with delay {}'.format(handle, delay))
-		timer = _SimpleTimer(delay * 1000, upon_timeout)
-		self.__timers.append(timer)
-
-		return timer
+		return self._timer.add_callback(asyncio.Handle(callback, args, self), delay)
 
 	def call_soon(self, callback, *args):
 		"""Register a callback to be run on the next iteration of the event loop."""
@@ -475,7 +476,7 @@ class _QEventLoop:
 		if isinstance(callback, asyncio.Handle):
 			assert not args
 			assert not isinstance(callback, asyncio.TimerHandle)
-			if callback.cancelled:
+			if callback._cancelled:
 				f = asyncio.Future()
 				f.set_result(None)
 				return f
diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py
index a7098c7..a8a6cc3 100644
--- a/tests/test_qeventloop.py
+++ b/tests/test_qeventloop.py
@@ -139,26 +139,27 @@ class TestCanRunTasksInExecutor:
 		logging.debug('start blocking task()')
 
 
-def test_can_execute_subprocess_primitive(loop):
-	"""Verify that a subprocess can be executed using low-level api."""
-	transport, protocol = loop.run_until_complete(
-		loop.subprocess_exec(
-			_SubprocessProtocol, sys.executable or 'python', '-c', 'import sys; sys.exit(5)',
-		),
-	)
-	loop.run_forever()
-	assert transport.get_returncode() == 5
+#def test_can_execute_subprocess_primitive(loop):
+#	"""Verify that a subprocess can be executed using low-level api."""
+#	transport, protocol = loop.run_until_complete(
+#		loop.subprocess_exec(
+#			_SubprocessProtocol, sys.executable or 'python', '-c', 'import sys; sys.exit(5)',
+#		),
+#	)
+#	loop.run_forever()
+#	assert transport.get_returncode() == 5
 
 
 def test_can_execute_subprocess(loop):
 	"""Verify that a subprocess can be executed."""
+	loop.set_debug(True)
 	@asyncio.coroutine
 	def mycoro():
 		process = yield from asyncio.create_subprocess_exec(
-			sys.executable or 'python', '-c', 'import sys; sys.exit(5)')
+			sys.executable or 'python', '-c', 'import sys; print("Hello World"); sys.exit(5)')
 		yield from process.wait()
 		assert process.returncode == 5
-	loop.run_until_complete(asyncio.wait_for(mycoro(), timeout=3))
+	loop.run_until_complete(mycoro())#asyncio.wait_for(mycoro(), timeout=3))
 
 
 def test_can_read_subprocess_primitive(loop):

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