<div>Hi tutors!</div><div><br></div><div>I&#39;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).</div><div>
<br>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&#39;s inbuilt threading module to allow me to scan multiple hosts concurrently.</div>
<div><br></div><div>I&#39;ve tried to design this function so that it won&#39;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..</div>
<div><br></div><div>I find that if the thread I&#39;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 &#39;FINISH&#39;. When I&#39;m spawning a thread that connects to a PC using WMI however, the output I get is more like this:</div>
<div><br></div><div>CONNECTING TO PC1 USING WMI</div><div>CONNECTING TO PC2 USING WMI</div><div>CONNECTING TO PC3 USING WMI</div><div>CONNECTING TO PC4 USING WMI</div><div>CONNECTING TO PC5 USING WMI</div><div>PC1 = SERVICE PACK3</div>
<div>FINISH</div><div>PC2 = SERVICE PACK3</div><div>PC3 = SERVICE PACK3</div><div>PC4 = SERVICE PACK3</div><div>PC5 = SERVICE PACK3</div><div><br></div><div>I&#39;m somewhat perplexed by this, because as I understood it:</div>
<div><br></div><div>if t.isAlive():   </div><div>        t.join() </div><div><br></div><div>checks to see if any threads are still running, and if they are, it will wait until the threads have terminated.</div><div><br></div>
<div>I&#39;m guessing that I&#39;m missing something here and I wondered if anyone has the time to point out the error of my ways?</div><div><br>Thanks :-)</div><div><br></div><div>def wmiem():</div><div>    DATABASELOC = shared.DATABASELOC    MAXTHREADS = shared.MAXTHREADS</div>
<div>    hoststate = [] #list of running threads</div><div>    try:</div><div>        conn = sqlite3.connect(DATABASELOC)</div><div>        c = conn.cursor()</div><div>    except:</div><div>        raw_input(&quot;Couldn&#39;t connect to the database. Press &#39;Enter&#39; to exit&quot;)</div>
<div>        exit()</div><div>    currenttime = time.time()</div><div>    anhourago = currenttime - 3600 #work out the time an hour ago by subtracting 3600 secs off unixtime</div><div>    tup = (anhourago,)</div><div>    c.execute(&quot;&quot;&quot;</div>
<div>    SELECT DISTINCT hostname FROM ping</div><div>    WHERE pingtime &gt; ? AND pingresult = 1;</div><div>    &quot;&quot;&quot;, tup)</div><div>    for computer in c.fetchall():</div><div>        for value in computer:</div>
<div>            while threading.activeCount() &gt; MAXTHREADS:</div><div>                time.sleep(0.5)</div><div>            print &#39;wmi started on &#39; + value </div><div>            t = threading.Thread(target=pingandwmi.whatsmyservicepack, args=(value,))</div>
<div>            hoststate.append(t)</div><div>            print threading.activeCount()</div><div>            t.start()</div><div>    if t.isAlive():   </div><div>        t.join() # wait till threads have finished.</div>
<div>        print &#39;FINISHED&#39;</div>