[#] Skrypt w Python a wiele wątków

( Ostatnio zmieniony wt., 08/07/2008 - 10:32 )
 

Bez wątków

pinger_no_thre­ads.py

#!/usr/bin/env python
#-*- coding:utf-8 -*-


import os
import re
import time
import sys

lifeline = re.compile(r"(\d) received")
report = ("No response", "Partial Response", "Alive")

print time.ctime()

for host in range(1, 200):
   ip = "10.1.1." + str(host)
   pingaling = os.popen("ping -q -c2 " + ip, "r")
   print "Testing ", ip,
   sys.stdout.flush()
   while 1:
      line = pingaling.readline()
      if not line: break
      igot = re.findall(lifeline, line)
      if igot:
           print report[int(igot[0])]

print time.ctime()

Z użyciem wątków

pinger_threads.py

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import re
import time
import sys
from threading import Thread

class testit(Thread):
   def __init__ (self, ip):
      Thread.__init__(self)
      self.ip = ip
      self.status = -1

   def run(self):
      pingaling = os.popen("ping -q -c2 " + self.ip, "r")
      while 1:
        line = pingaling.readline()
        if not line: break
        igot = re.findall(testit.lifeline, line)
        if igot:
           self.status = int(igot[0])

testit.lifeline = re.compile(r"(\d) received")
report = ("No response","Partial Response","Alive")

print time.ctime()

pinglist = []

for host in range(1, 200):
   ip = "10.1.1." + str(host)
   current = testit(ip)
   pinglist.append(current)
   current.start()

for pingle in pinglist:
   pingle.join()
   print "Status from ", pingle.ip, "is", report[pingle.status]

print time.ctime()

W wyniku zobaczymy:

...
Status from  10.1.1.88 is No response
Status from  10.1.1.89 is No response
Status from  10.1.1.90 is No response
Status from  10.1.1.91 is No response
Status from  10.1.1.92 is No response
Status from  10.1.1.93 is No response
Status from  10.1.1.94 is No response
Status from  10.1.1.95 is No response
Status from  10.1.1.96 is No response
Status from  10.1.1.97 is No response
Status from  10.1.1.98 is No response
Status from  10.1.1.99 is No response
Status from  10.1.1.100 is Alive
Status from  10.1.1.101 is No response
Status from  10.1.1.102 is No response
Status from  10.1.1.103 is No response
Status from  10.1.1.104 is No response
Status from  10.1.1.105 is No response
Status from  10.1.1.106 is No response
...

Polecenie ps aux pokaże nam:

...
unsername    9237  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.98
unsername    9238  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.130
unsername    9239  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.130
unsername    9240  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.143
unsername    9241  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.143
unsername    9242  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.121
unsername    9243  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.121
unsername    9244  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.144
unsername    9245  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.144
unsername    9246  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.77
unsername    9247  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.122
unsername    9248  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.77
unsername    9249  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.122
unsername    9250  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.115
unsername    9251  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.134
unsername    9252  0.0  0.0   1872   536 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.115
unsername    9253  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.134
unsername    9254  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.135
unsername    9255  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.117
unsername    9256  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.135
unsername    9257  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.117
unsername    9258  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.111
unsername    9259  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.111
unsername    9260  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.127
unsername    9261  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.128
unsername    9262  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.127
unsername    9263  0.0  0.0   1772   480 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.140
unsername    9264  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.128
unsername    9265  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.141
unsername    9266  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.140
unsername    9267  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.120
unsername    9268  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.141
unsername    9269  0.0  0.0   1772   484 pts/0    S+   10:19   0:00 sh -c ping -q -c2 10.1.1.142
unsername    9270  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.120
unsername    9271  0.0  0.0   1872   532 pts/0    S+   10:19   0:00 ping -q -c2 10.1.1.142
...

Czas działania polecenia ping z użyciem wątków jest zdecydowanie znacznie, znacznie krótszy niż w normalnej wersji bez wątków. Średnio czas trwania pinga to 3s. Wystarczy teraz pomnożyć to przez liczbę hostów do pingowania oraz odjąć 3s i mamy różnicę jaka powstaje przy i bez użycia wątków.

Źródło: http://www.wellho.net/…example.html

5
Twoja ocena: Brak Średnio: 5 (1 vote)