From 9801b4189d014758b74d1f334cc2bf0273d74787 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Mon, 5 Nov 2018 21:14:07 +0100 Subject: [PATCH] parallelize router queries --- inventory_provider/router_interfaces.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/inventory_provider/router_interfaces.py b/inventory_provider/router_interfaces.py index 21d6eb11..4b17f10b 100644 --- a/inventory_provider/router_interfaces.py +++ b/inventory_provider/router_interfaces.py @@ -1,6 +1,7 @@ import contextlib import json import re +from multiprocessing import Pool import click import jsonschema @@ -217,6 +218,10 @@ def get_router_interfaces(router): } +def get_router_interfaces_l(router): + return list(get_router_interfaces(router)) + + @click.command() @click.option( "--config", @@ -226,15 +231,13 @@ def get_router_interfaces(router): default=open("config.json"), callback=_validate_config) def cli(config): + pool = Pool(20) with open("routers_community.conf") as f: - for r in load_routers(f): - - with connection(config["alarms-db"]) as c: - _db_test(c, r) - - print(r["hostname"]) - for i in get_router_interfaces(r): - print("\t%r" % i) + routers = list(load_routers(f)) + for r, i in zip(routers, pool.map(get_router_interfaces_l, routers)): + print(r["hostname"]) + for ifc in i: + print("\t%r" % ifc) if __name__ == "__main__": -- GitLab