Оптимизация производительности кода python

Есть скрипт:

import datetime, db, smbclient
from pysnmp.hlapi import *
from tendo import singleton
######### Enforce one running instance ###########################################
me = singleton.SingleInstance()
mongo = list(db.coll2.find())
######### Update the current map ##################################################
server="192.168.133.2"
share="dhcp$"
username = "ldapreader"
password = "######"
smb = smbclient.SambaClient(server,share,username,password)
d = smb.open('/clients.txt')
data = d.readlines()




for currentSection in db.sections[1:]:
    portDensity = db.config.getint(currentSection, 'portDensity')
    stackSwitches = db.config.getint(currentSection, 'stackSwitches')
    IPAddress = db.config.get(currentSection, 'IPAddress')
    communityString = db.config.get(currentSection, 'communityString')
    snmpPort = db.config.get(currentSection, 'snmpPort')
    for (errorIndication,
            errorStatus,
            errorIndex,
            varBinds) in bulkCmd(SnmpEngine(),
                            CommunityData(communityString),
                            UdpTransportTarget((IPAddress, snmpPort)),
                            ContextData(),
                            0, 25,
                            ObjectType(ObjectIdentity('BRIDGE-MIB', 'dot1dTpFdbPort')),
                lexicographicMode=False,
                            lookupMib=True):
        if errorIndication:
                Exception(errorIndication)
        elif errorStatus:
                Exception(errorStatus)
        else:

                for varBind in varBinds:
                        unit = varBind[1] / portDensity + 1
                        port = varBind[1] % portDensity
                        switchName = str(currentSection) + "." + str(unit) + "." + str(port)
                        sName = str(currentSection)
                        ports = str(port)
                        macAddress = varBind[0].prettyPrint()[-18:-1]
                        for p in data:
                            m = p[:15]
                            t = p[34:52]
                            n = p[82:]
                            f = t.replace('-', ':')
                            mac_ip = f.strip()
                            ip_ip = m.strip()
                            name_ip = n.strip()
                            for i in mongo:
                                s = str.lower(i['Mac Prefix'])
                                d = macAddress[0:len(s)]
                                g = i['Vendor Name']
                                if (d == s and port != 0 and unit <= stackSwitches and macAddress == mac_ip):
                                         db.coll.insert_one( {"mac" : macAddress,"switch_name" : switchName,"port" : ports,"manufacter" : g,"ip" : ip_ip,"name" : name_ip, "date_updated" : datetime.datetime.now() })

Работает очень медленно, может у кого то будут какие-либо советы относительно оптимизации кода и ускорения быстродействия?


Ответы (0 шт):