Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio question
#1
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
Reply
#2
Does openconnexion work when run by itself?
Reply
#3
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.
Reply
#4
Looking at this web page it appears that json servers run in their own thread.

http://www.geekdroppings.com/2014/06/24/...sonrpclib/

If this is true, you would not get any benefit from trying to run them asynchronously. They are already running that way.
Reply
#5
Thank you for the link I am going to read it right now

Best regards and keep safe
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020