asyncio question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: asyncio question (/thread-26058.html) |
asyncio question - fdFrench - Apr-20-2020 Hello It is my first post. I would like to use asyncio for a network module. The purpose is to send the same request to multiple devices and waiting for the answer from all. I tried the following code but it is not working. import asyncio from datetime import datetime from jsonrpclib import Server import os, sys, re import time import json from netaddr import * # username and password for the connection username = '' password = '' deviceNotConnected=[] devices =['spine1','spine2','leaf1-A','leaf1-B','leaf2-A','leaf2-B','leaf3-A','leaf4'] commandes=['show lldp neighbors detail', 'show version', 'show interfaces status', 'show ip interface', 'show ip bgp summary'] # Connection to the switch async def openconnexion(device,username,password,cde): switch = Server("http://%s:%s@%s/command-api" %(username,password,device)) try: result = switch.runCmds(version = 1, cmds = cde) except: deviceNotConnected.append(device) pass async def main(): start_time_Final = datetime.now() for device in devices: await asyncio.gather(openconnexion(device,username,password,commandes)) totalFinal = datetime.now() - start_time_Final print (totalFinal) asyncio.run(main())Regards RE: asyncio question - deanhystad - Apr-20-2020 Does openconnexion work when run by itself? RE: asyncio question - fdFrench - Apr-20-2020 Yes, the program is working fine. The main question is : Am I using an asynchronous mode for querying my device? If I do simple loop without asyncio, I have exactly the same time for the process. RE: asyncio question - deanhystad - Apr-20-2020 Looking at this web page it appears that json servers run in their own thread. http://www.geekdroppings.com/2014/06/24/simple-python-json-server-based-on-jsonrpclib/ If this is true, you would not get any benefit from trying to run them asynchronously. They are already running that way. RE: asyncio question - fdFrench - Apr-20-2020 Thank you for the link I am going to read it right now Best regards and keep safe |