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: lib/portage/tests/util/futures/, lib/portage/util/futures/
Date: Mon, 24 Sep 2018 06:12:11 +0000 (UTC)	[thread overview]
Message-ID: <1537760492.de0b60ff277311e780102131dce3111b4db1c196.zmedico@gentoo> (raw)

commit:     de0b60ff277311e780102131dce3111b4db1c196
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sat Jul 28 13:53:11 2018 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Mon Sep 24 03:41:32 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=de0b60ff

Add _sync_decorator module

Add functions that decorate coroutine methods and functions for
synchronous usage, allowing coroutines to smoothly blend with
synchronous code. This eliminates clutter that might otherwise
discourage the proliferation of coroutine usage for I/O bound tasks.

In the next commit, _sync_decorator will be used for smooth
integration of new classes that have coroutine methods.

Bug: https://bugs.gentoo.org/662070
Reviewed-by: Brian Dolbec <dolsen <AT> gentoo.org>
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 .../tests/util/futures/test_compat_coroutine.py    | 14 ++++++
 lib/portage/util/futures/_sync_decorator.py        | 54 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/lib/portage/tests/util/futures/test_compat_coroutine.py b/lib/portage/tests/util/futures/test_compat_coroutine.py
index b6f75b1a2..f96aa9be5 100644
--- a/lib/portage/tests/util/futures/test_compat_coroutine.py
+++ b/lib/portage/tests/util/futures/test_compat_coroutine.py
@@ -6,6 +6,7 @@ from portage.util.futures.compat_coroutine import (
 	coroutine,
 	coroutine_return,
 )
+from portage.util.futures._sync_decorator import _sync_decorator, _sync_methods
 from portage.tests import TestCase
 
 
@@ -161,3 +162,16 @@ class CompatCoroutineTestCase(TestCase):
 		loop.run_until_complete(asyncio.wait([writer, reader]))
 
 		self.assertEqual(reader.result(), values)
+
+		# Test decoration of coroutine methods and functions for
+		# synchronous usage, allowing coroutines to smoothly
+		# blend with synchronous code.
+		sync_cubby = _sync_methods(cubby, loop=loop)
+		sync_reader = _sync_decorator(reader_coroutine, loop=loop)
+		writer = asyncio.ensure_future(writer_coroutine(cubby, values, None), loop=loop)
+		self.assertEqual(sync_reader(cubby, None), values)
+		self.assertTrue(writer.done())
+
+		for i in range(3):
+			sync_cubby.write(i)
+			self.assertEqual(sync_cubby.read(), i)

diff --git a/lib/portage/util/futures/_sync_decorator.py b/lib/portage/util/futures/_sync_decorator.py
new file mode 100644
index 000000000..02a0963a7
--- /dev/null
+++ b/lib/portage/util/futures/_sync_decorator.py
@@ -0,0 +1,54 @@
+# Copyright 2018 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+import functools
+
+import portage
+portage.proxy.lazyimport.lazyimport(globals(),
+	'portage.util.futures:asyncio',
+)
+
+
+def _sync_decorator(func, loop=None):
+	"""
+	Decorate an asynchronous function (either a corouting function or a
+	function that returns a Future) with a wrapper that runs the function
+	synchronously.
+	"""
+	loop = asyncio._wrap_loop(loop)
+	@functools.wraps(func)
+	def wrapper(*args, **kwargs):
+		return loop.run_until_complete(func(*args, **kwargs))
+	return wrapper
+
+
+def _sync_methods(obj, loop=None):
+	"""
+	For use with synchronous code that needs to interact with an object
+	that has coroutine methods, this function generates a proxy which
+	conveniently converts coroutine methods into synchronous methods.
+	This allows coroutines to smoothly blend with synchronous
+	code, eliminating clutter that might otherwise discourage the
+	proliferation of coroutine usage for I/O bound tasks.
+	"""
+	loop = asyncio._wrap_loop(loop)
+	return _ObjectAttrWrapper(obj,
+		lambda attr: _sync_decorator(attr, loop=loop)
+		if asyncio.iscoroutinefunction(attr) else attr)
+
+
+class _ObjectAttrWrapper(portage.proxy.objectproxy.ObjectProxy):
+
+	__slots__ = ('_obj', '_attr_wrapper')
+
+	def __init__(self, obj, attr_wrapper):
+		object.__setattr__(self, '_obj', obj)
+		object.__setattr__(self, '_attr_wrapper', attr_wrapper)
+
+	def __getattribute__(self, attr):
+		obj = object.__getattribute__(self, '_obj')
+		attr_wrapper = object.__getattribute__(self, '_attr_wrapper')
+		return attr_wrapper(getattr(obj, attr))
+
+	def _get_target(self):
+		return object.__getattribute__(self, '_obj')


             reply	other threads:[~2018-09-24  6:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-24  6:12 Zac Medico [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-09-03 20:23 [gentoo-commits] proj/portage:master commit in: lib/portage/tests/util/futures/, lib/portage/util/futures/ 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=1537760492.de0b60ff277311e780102131dce3111b4db1c196.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