ich habe ein Skript geschrieben, welches mehrere ping Prüfungen parallel ausführen soll. Mein Zähler num_threads soll mir anzeigen, wieviele Threads aktiv sind und es sollen maximal 3 Threads erlaubt sein.
Aber der Zähler bleibt nicht zwischen 1 und 3.
Kann mir bitte jemand helfen, warum sich die Variable num_threads über 3 erhöht?
./create_idb_db.py
create idb db file
vm7472 is down, number of threads 1
vm7528 is up, number of threads 4
vm7571 is up, number of threads 6
vm6731 is up, number of threads 12
vm6738 is up, number of threads 12
vm6732 is up, number of threads 12
vm6739 is up, number of threads 12
...
vm7964 is up, number of threads 365
vm7966 is up, number of threads 366
vm0903 is up, number of threads 368
vm7963 is up, number of threads 369
vm7960 is up, number of threads 369
vm7967 is up, number of threads 370
Code: Alles auswählen
def ping(hostname):
global num_threads, thread_started
lock.acquire()
num_threads += 1
thread_started = True
lock.release()
response = os.system("ping -I 10.110.0.1 -c 1 " + hostname + " >> /dev/null 2>&1")
if response == 0:
print hostname, 'is up, number of threads', num_threads
status = 0
else:
response = os.system("ping -I 10.110.0.1 -c 3 " + hostname + " >> /dev/null 2>&1")
if response == 0:
print hostname, 'is up, number of threads', num_threads
status = 0
else:
print hostname, 'is down, number of threads', num_threads
status = 1
cp.set(cat,"ping",str(status))
lock.acquire()
num_threads -= 1
lock.release()
cp = ConfigParser.SafeConfigParser()
cp.read(idb_file)
num_threads = 0
thread_started = False
lock = thread.allocate_lock()
for cat in cp.sections():
device_section, hostname = cat.split(':')
if device_section == "server":
try:
thread.start_new_thread(ping, (hostname,))
while not thread_started:
pass
while (num_threads > 0) and (num_threads < 4):
pass
except:
print "Error: unable to start thread"