Skip to content

Instantly share code, notes, and snippets.

@zmedico
Last active May 17, 2018 23:04
Show Gist options
  • Save zmedico/e5ac1f48686f6588b3fa23129bb122cc to your computer and use it in GitHub Desktop.
Save zmedico/e5ac1f48686f6588b3fa23129bb122cc to your computer and use it in GitHub Desktop.
async aux_get multi example usage
#!/usr/bin/env python
from __future__ import print_function
import argparse
import functools
import multiprocessing
import operator
import portage
from portage.util.futures.iter_completed import (
async_iter_completed,
)
from portage.util.futures.unix_events import (
DefaultEventLoopPolicy,
)
def handle_result(cpv, future):
metadata = dict(zip(portage.auxdbkeys, future.result()))
print(cpv)
for k, v in sorted(metadata.items(),
key=operator.itemgetter(0)):
if v:
print('\t{}: {}'.format(k, v))
print()
def future_generator(repo_location, loop=None):
portdb = portage.portdb
for cp in portdb.cp_all(trees=[repo_location]):
for cpv in portdb.cp_list(cp, mytree=repo_location):
future = portdb.async_aux_get(
cpv,
portage.auxdbkeys,
mytree=repo_location,
loop=loop,
)
future.add_done_callback(
functools.partial(handle_result, cpv))
yield future
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--repo',
action='store',
default='gentoo',
)
parser.add_argument(
'--jobs',
action='store',
type=int,
default=multiprocessing.cpu_count(),
)
parser.add_argument(
'--load-average',
action='store',
type=float,
default=multiprocessing.cpu_count(),
)
args = parser.parse_args()
try:
repo_location = portage.settings.repositories.\
get_location_for_name(args.repo)
except KeyError:
parser.error('unknown repo: {}\navailable repos: {}'.\
format(args.repo, ' '.join(sorted(
repo.name for repo in
portage.settings.repositories))))
policy = DefaultEventLoopPolicy()
loop = policy.get_event_loop()
try:
for future_done_set in async_iter_completed(
future_generator(repo_location, loop=loop),
max_jobs=args.jobs,
max_load=args.load_average,
loop=loop):
loop.run_until_complete(future_done_set)
finally:
loop.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment