I want to access two Cisco IOS switches (192.168.2.11 and 2.12) and get facts from them (with help of 'pyntc' library) concurrently using asyncio, I'm using Python 3.8.2.
I did a test in another script WITHOUT using asyncio, it took 25 seconds in total to get the work done, each switch took roughtly 12.5 seconds to be processed.
I was expecting that below script should take around 13.5 seconds in total (with an additional second from "await asyncio.sleep(1)") to get the facts from both switches, but it ended up taking 27 seconds in total.
I'm new to asyncio, and couldn't figure out why it's not working as expected.
import asyncio
from pyntc import ntc_device as NTC
import json
import time
async def SW1():
SW = NTC(host='192.168.2.11', username='user', password='pass', device_type='cisco_ios_ssh')
SW.open()
print ('Successfully logined to 192.168.2.11.')
await asyncio.sleep(1)
print (json.dumps(SW.facts, indent=4))
async def SW2():
SW = NTC(host='192.168.2.12', username='user', password='pass', device_type='cisco_ios_ssh')
SW.open()
print ('Successfully logined to 192.168.2.12.')
await asyncio.sleep(1)
print (json.dumps(SW.facts, indent=4))
async def main():
task1 = asyncio.create_task(SW1())
task2 = asyncio.create_task(SW2())
start_time = time.time()
await task1
await task2
print ("Time elapsed: %.2f"%(time.time()-start_time))
asyncio.run(main())