How to do exception handling on multiprocessing python -
i use multiprocessing in python code.
python code imports pysnmp , multiprocessing.
half of time code runs smoothly.
unfortunately half of other time code doesn't work , shows exception "pyasn1.error.pyasn1 error: type tagset".
code first creates "multiprocessing.dummy.pool(numofthreads)" number of threads.
calls "p.map(sendsnmpget, [iprange + '.' + str(x) x in range(1,256)])" takes function "sendsnmpget" thread function, , calls function 255 times values of "1-255".
code:
from pysnmp.entity.rfc3413.oneliner import cmdgen pysnmp.proto.rfc1902 import integer, ipaddress, octetstring import multiprocessing.dummy import multiprocessing def snmpget(ip, community, oid, version = 1): generator = cmdgen.commandgenerator() comm_data = cmdgen.communitydata('server', community, version) # 1 means version snmp v2c transport = cmdgen.udptransporttarget((ip, 161),timeout=0.5,retries=2) real_fun = getattr(generator, 'getcmd') res = (errorindication, errorstatus, errorindex, varbinds)\ = real_fun(comm_data, transport, oid) if (errorstatus == 2): #is there endofmib() error return elif (errorindication none or errorstatus true): open("maprinter.txt", "a") myfile: myfile.write("date is: " + datetime.datetime.now().strftime("%d-%m-%y %h:%m:%s") + " ip is: " + ip + ", response: %s\n" % varbinds) print("ip is: " + ip + ", response: %s\n" % varbinds) return def sendsnmpget(ip): return snmpget(ip, 'public', '1.3.6.1.2.1.43.5.1.1.1.1', 1) def snmp_range(iprange, start, end): num_threads = 4 * multiprocessing.cpu_count() p = multiprocessing.dummy.pool(num_threads) p.map(sendsnmpget, [iprange + '.' + str(x) x in range(start,end)]) def getipsrange(ips): print('starting scan') if __name__ == "__main__": if(ips != ''): #if there ips in registry ip in range(len(ips)): snmp_range(ips[ip], 1, 256) print('ending scan') getipsrange('10.0.0')
despite use of pysnmp think exception occurs due lack of proper coding of multiprocessing. how change code handle exception or better, prevent happening?
Comments
Post a Comment