commit:     82759203b9b39389de9ab27aeae58319b604f1ae
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 10 15:16:21 2018 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Wed Apr 11 01:44:34 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=82759203

async_aux_get: support asyncio via _PortageEventLoopPolicy (bug 649588)

Support portage's internal EventLoop as well as the _PortageEventLoop
asyncio compatibility wrapper, by using the respective _loop and
_asyncio_wrapper attributes where appropriate.

Example usage for coroutine with PEP 492 async and await syntax:

  import asyncio
  import portage
  from portage.exception import PortageKeyError
  from portage.util.futures.unix_events import DefaultEventLoopPolicy

  asyncio.set_event_loop_policy(DefaultEventLoopPolicy())

  async def aux_get_demo():
    portdb = portage.portdb
    for cpv in portdb.cp_list('sys-apps/portage'):
      try:
        result = await portdb.async_aux_get(cpv, portage.auxdbkeys)
      except PortageKeyError as e:
        # aux_get failed
        print('error:', cpv, e)
      else:
        print(cpv, result)

  asyncio.get_event_loop().run_until_complete(aux_get_demo())

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

 pym/portage/dbapi/porttree.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pym/portage/dbapi/porttree.py b/pym/portage/dbapi/porttree.py
index 910e90e6f..951e5760a 100644
--- a/pym/portage/dbapi/porttree.py
+++ b/pym/portage/dbapi/porttree.py
@@ -611,6 +611,7 @@ class portdbapi(dbapi):
                # Callers of this method certainly want the same event loop to
                # be used for all calls.
                loop = loop or global_event_loop()
+               loop = getattr(loop, '_asyncio_wrapper', loop)
                future = loop.create_future()
                cache_me = False
                if myrepo is not None:
@@ -665,7 +666,7 @@ class portdbapi(dbapi):
 
                proc = EbuildMetadataPhase(cpv=mycpv,
                        ebuild_hash=ebuild_hash, portdb=self,
-                       repo_path=mylocation, scheduler=loop,
+                       repo_path=mylocation, scheduler=loop._loop,
                        settings=self.doebuild_settings)
 
                proc.addExitListener(functools.partial(self._aux_get_return,

Reply via email to