# SVN changeset patch # User imanay # Date 2013-04-25 22:51:01.346191 # Revision 136 Index: trunk/absroot/source/abspy/test/threading/test4.py =================================================================== diff --git a/trunk/absroot/source/abspy/test/threading/test4.py b/trunk/absroot/source/abspy/test/threading/test4.py new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/absroot/source/abspy/test/threading/test4.py (revision 136) @@ -0,0 +1,20 @@ +import threading, zipfile + +class AsyncZip(threading.Thread): + def __init__(self, infile, outfile): + threading.Thread.__init__(self) + self.infile = infile + self.outfile = outfile + def run(self): + f = zipfile.ZipFile(self.outfile, 'w', + zipfile.ZIP_DEFLATED) + f.write(self.infile) + f.close() + print 'Finished background zip of: ', self.infile + +background = AsyncZip('mydata.txt', 'myarchive.zip') +background.start() +print 'The main program continues to run in foreground.' + +background.join() # Wait for background task to finish +print 'Main program waited until background was done.' Index: trunk/absroot/source/abspy/test/threading/test5.py =================================================================== diff --git a/trunk/absroot/source/abspy/test/threading/test5.py b/trunk/absroot/source/abspy/test/threading/test5.py new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/absroot/source/abspy/test/threading/test5.py (revision 136) @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +import subprocess +import threading + +class Pinger(object): + status = {'alive': [], 'dead': []} # Populated while we are running + hosts = [] # List of all hosts/ips in our input queue + + # How many ping process at the time. + thread_count = 4 + + # Lock object to keep track the threads in loops, where it can potentially be race conditions. + lock = threading.Lock() + + def ping(self, ip): + # Use the system ping command with count of 1 and wait time of 1. + ret = subprocess.call(['ping', '-c', '1', '-W', '1', ip], + stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w')) + + return ret == 0 # Return True if our ping command succeeds + + def pop_queue(self): + ip = None + + self.lock.acquire() # Grab or wait+grab the lock. + + if self.hosts: + ip = self.hosts.pop() + + self.lock.release() # Release the lock, so another thread could grab it. + + return ip + + def dequeue(self): + while True: + ip = self.pop_queue() + + if not ip: + return None + + result = 'alive' if self.ping(ip) else 'dead' + self.status[result].append(ip) + + def start(self): + threads = [] + + for i in range(self.thread_count): + # Create self.thread_count number of threads that together will + # cooperate removing every ip in the list. Each thread will do the + # job as fast as it can. + t = threading.Thread(target=self.dequeue) + t.start() + threads.append(t) + + # Wait until all the threads are done. .join() is blocking. + [ t.join() for t in threads ] + + return self.status + +if __name__ == '__main__': + ping = Pinger() + ping.thread_count = 8 + ping.hosts = [ + '10.10.10.97', '10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.0', '10.0.0.255', '10.0.0.100', + 'google.com', 'github.com', 'nonexisting', '127.0.1.2', '*not able to ping!*', '8.8.8.8' + ] + + print ping.start() \ No newline at end of file