nornir-automation / nornir

Pluggable multi-threaded framework with inventory management to help operate collections of devices
https://nornir.readthedocs.io/
Apache License 2.0
1.37k stars 233 forks source link

Shell prompt hangs once Result is printed when at least one host fails if InitNornir is done inside main() #884

Open deep-blue-pulsar opened 8 months ago

deep-blue-pulsar commented 8 months ago

Whenever an NXOS device using the ssh driver in the inventory is unreachable, tasks will complete normally for all reachable hosts and fail for the unreachable one but once the execution is complete, the shell prompt will be locked for input for about 45 seconds per successful NXOS host in the inventory. So if there are 10 successful hosts, and a single one fails, I'll be waiting at least 7 min to get my prompt back unless I send an interrupt signal.

I've stripped down my code to the bare minimum to try narrowing down the issue and arrived to this:

def main():

    nr = InitNornir("config.yaml")
    load_dotenv()
    nr.inventory.defaults.username = os.getenv("USERNAME")
    nr.inventory.defaults.password = os.getenv("PASSWORD")

    result = nr.run(task=napalm_get, getters=["config"])

    print_result(result)

if __name__ == "__main__":
    main()

If I take the code out of the main() function I can't reproduce it. If I also create the Nornir object inside a class in another module and instantiate an object of that class inside a main function the issue doesn't seem to happen.

Any idea why this happens? What am I doing wrong here?