public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Zac Medico" <zmedico@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/tests/ebuild/, pym/_emerge/
Date: Thu, 26 Apr 2018 03:45:37 +0000 (UTC)	[thread overview]
Message-ID: <1524712762.71a5a82313226f7be0d966d49392a53139a96f6b.zmedico@gentoo> (raw)

commit:     71a5a82313226f7be0d966d49392a53139a96f6b
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 24 02:47:11 2018 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Thu Apr 26 03:19:22 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=71a5a823

AsynchronousTask: add async_wait() method (bug 653856)

Since the AsynchronousTask.wait() method is prone to event loop
recursion, deprecate it, and add an async_wait() method method to
replace it. Instead of using task.wait() in order to implicitly run
the event loop, now loop.run_until_complete(task.async_wait()) will
be used to explicitly run the event loop. This explicit approach will
make it more obvious when code will trigger event loop recursion
which would not be compatible with asyncio's default event loop.

Bug: https://bugs.gentoo.org/653856

 pym/_emerge/AsynchronousTask.py             | 23 +++++++++++++++++++++++
 pym/portage/tests/ebuild/test_ipc_daemon.py |  2 +-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/pym/_emerge/AsynchronousTask.py b/pym/_emerge/AsynchronousTask.py
index e29324440..7d2e6253b 100644
--- a/pym/_emerge/AsynchronousTask.py
+++ b/pym/_emerge/AsynchronousTask.py
@@ -29,6 +29,26 @@ class AsynchronousTask(SlotObject):
 		self._start_hook()
 		self._start()
 
+	def async_wait(self):
+		"""
+		Wait for returncode asynchronously. Notification is available
+		via the add_done_callback method of the returned Future instance.
+
+		@returns: Future, result is self.returncode
+		"""
+		waiter = self.scheduler.create_future()
+		exit_listener = lambda self: waiter.set_result(self.returncode)
+		self.addExitListener(exit_listener)
+		waiter.add_done_callback(lambda waiter:
+			self.removeExitListener(exit_listener) if waiter.cancelled() else None)
+		if self.returncode is not None:
+			# If the returncode is not None, it means the exit event has already
+			# happened, so use _async_wait() to guarantee that the exit_listener
+			# is called. This does not do any harm because a given exit listener
+			# is never called more than once.
+			self._async_wait()
+		return waiter
+
 	def _start(self):
 		self.returncode = os.EX_OK
 		self.wait()
@@ -47,6 +67,9 @@ class AsynchronousTask(SlotObject):
 		return self.returncode
 
 	def wait(self):
+		"""
+		Deprecated. Use async_wait() instead.
+		"""
 		if self.returncode is None:
 			if not self._waiting:
 				self._waiting = True

diff --git a/pym/portage/tests/ebuild/test_ipc_daemon.py b/pym/portage/tests/ebuild/test_ipc_daemon.py
index bc18cdf64..e6da51a76 100644
--- a/pym/portage/tests/ebuild/test_ipc_daemon.py
+++ b/pym/portage/tests/ebuild/test_ipc_daemon.py
@@ -157,6 +157,6 @@ class IpcDaemonTestCase(TestCase):
 		try:
 			task_scheduler.start()
 			event_loop.run_until_complete(self._run_done)
-			task_scheduler.wait()
+			event_loop.run_until_complete(task_scheduler.async_wait())
 		finally:
 			timeout_handle.cancel()


             reply	other threads:[~2018-04-26  3:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-26  3:45 Zac Medico [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-02-14  2:36 [gentoo-commits] proj/portage:master commit in: pym/portage/tests/ebuild/, pym/_emerge/ Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1524712762.71a5a82313226f7be0d966d49392a53139a96f6b.zmedico@gentoo \
    --to=zmedico@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox