Threads and snmp?

Cliff Daniel cdaniel at level3.net
Sun May 9 12:42:13 EDT 1999


I'm trying to find out a few things about threaded python using the snmp
module.  Bare with me here
as this is my first python script.  Basically this scripts purpose it to
pull an oid from about 300 hosts.
Upon startup each thread is assigned multiple hosts in the MultipleDict
hosts[] variable.  All that
is working fine and starts all threads and they do their jobs without a
problem.  The only thing is that
running this with 1 thread is faster than , say, 15 threads.  Is this a
design flaw or is there something
with the snmp module that is limiting the number of concurrent polls going
on ?  Most of the code
is below, I've left out other stuff but here is the 'time' output of a few
threads.  This is polling 300
hosts.

1 thread  = 14.13 secs
3 threads = 15.4 secs
5 threads = 14.17 secs
20 threads = 14.97 secs

I've also noticed that sometimes the snmp module reports 'invalid peer' with
no frequency.  Sometimes
is does sometimes it doesn't.

Any ideas?

############################################################################
##
# WorkerThread()
############################################################################
##
def WorkerThread(tid):
    global Active
    global Sessions

    Total = 0

    print 'Thread %2d reporting for duty!' % tid

    for targ in hosts[tid]:
        s = snmp.open(targ, COMM, timeout=100000)

        try:
            res = s.get(SESSIONS)
            result = res.value
            Total = Total + string.atoi(result)

        except snmp.SnmpTimeout:
            result = "Timeout"

        print '[tid:%02d] %-30s : %3s current sessions' % (tid, targ,
result)

    active_mutex.acquire()
    Active = Active - 1

    if (Active == 0):
        wait.release()

    active_mutex.release()

    print "Finished thread", `tid`
    session_mutex.acquire()
    Sessions = Sessions + Total
    session_mutex.release()
    thread.exit_thread()

############################################################################
##
# Main()
############################################################################
##
def Main():
    global Active
    global NumThreads

    NumThreads = string.atoi(sys.argv[1])
    Active = NumThreads
    print 'Threads = ', NumThreads

    ReadConf()

    for n in range(NumThreads):
        thread.start_new_thread(WorkerThread, (n,))

    wait.acquire()
    print "Total Sessions:", Sessions







More information about the Python-list mailing list