[Tutor] Waiting until a thread ends

lists lists at justuber.com
Thu Mar 17 22:47:42 CET 2011


Hi tutors!

I'm not a hard-core programmer but have used Python in systems
administration (it makes sense to me to use a scripting language which
supports both Windows and Unix).

The code below is an excerpt from a module I have written to collect
information about hosts on my network and populate a database with this
data. I have a couple of thousand PCs to check, so decided to try and use
Python's inbuilt threading module to allow me to scan multiple hosts
concurrently.

I've tried to design this function so that it won't end until all of the
threads it spawns have completed. I want it to work like this so I can be
sure that each step of the script has completed before moving onto the next
step..

I find that if the thread I'm calling uses subprocess.popen (for instance, a
function using the windows ping command to collect information), that
requests for ping are sent, and are results are returned before the script
prints 'FINISH'. When I'm spawning a thread that connects to a PC using WMI
however, the output I get is more like this:

CONNECTING TO PC1 USING WMI
CONNECTING TO PC2 USING WMI
CONNECTING TO PC3 USING WMI
CONNECTING TO PC4 USING WMI
CONNECTING TO PC5 USING WMI
PC1 = SERVICE PACK3
FINISH
PC2 = SERVICE PACK3
PC3 = SERVICE PACK3
PC4 = SERVICE PACK3
PC5 = SERVICE PACK3

I'm somewhat perplexed by this, because as I understood it:

if t.isAlive():
        t.join()

checks to see if any threads are still running, and if they are, it will
wait until the threads have terminated.

I'm guessing that I'm missing something here and I wondered if anyone has
the time to point out the error of my ways?

Thanks :-)

def wmiem():
    DATABASELOC = shared.DATABASELOC    MAXTHREADS = shared.MAXTHREADS
    hoststate = [] #list of running threads
    try:
        conn = sqlite3.connect(DATABASELOC)
        c = conn.cursor()
    except:
        raw_input("Couldn't connect to the database. Press 'Enter' to exit")
        exit()
    currenttime = time.time()
    anhourago = currenttime - 3600 #work out the time an hour ago by
subtracting 3600 secs off unixtime
    tup = (anhourago,)
    c.execute("""
    SELECT DISTINCT hostname FROM ping
    WHERE pingtime > ? AND pingresult = 1;
    """, tup)
    for computer in c.fetchall():
        for value in computer:
            while threading.activeCount() > MAXTHREADS:
                time.sleep(0.5)
            print 'wmi started on ' + value
            t = threading.Thread(target=pingandwmi.whatsmyservicepack,
args=(value,))
            hoststate.append(t)
            print threading.activeCount()
            t.start()
    if t.isAlive():
        t.join() # wait till threads have finished.
        print 'FINISHED'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110317/ddc3aa8e/attachment.html>


More information about the Tutor mailing list