21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Threads 149

if __name__ == '__main__':

# Crie uma lista com um objeto de thread para cada IP

monitores = []

for i in range(1, 11):

ip = '10.10.10.%d' % i

monitores.append(Monitor(ip))

# Execute as Threads

for monitor in monitores:

monitor.start()

# A thread principal continua enquanto

# as outras threads executam o ping

# para os endereços da lista

# Verifique a cada segundo

# se as threads acabaram

ping = True

while ping:

ping = False

for monitor in monitores:

if monitor.status == None:

ping = True

break

time.sleep(1)

# Imprima os resultados no final

for monitor in monitores:

if monitor.status:

print '%s no ar' % monitor.ip

else:

print '%s fora do ar' % monitor.ip

Saída:

10.10.10.1 no ar

10.10.10.2 no ar

10.10.10.3 no ar

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!