@@ -0,0 +1,20 | |||||
|
1 | import threading, zipfile | |||
|
2 | ||||
|
3 | class AsyncZip(threading.Thread): | |||
|
4 | def __init__(self, infile, outfile): | |||
|
5 | threading.Thread.__init__(self) | |||
|
6 | self.infile = infile | |||
|
7 | self.outfile = outfile | |||
|
8 | def run(self): | |||
|
9 | f = zipfile.ZipFile(self.outfile, 'w', | |||
|
10 | zipfile.ZIP_DEFLATED) | |||
|
11 | f.write(self.infile) | |||
|
12 | f.close() | |||
|
13 | print 'Finished background zip of: ', self.infile | |||
|
14 | ||||
|
15 | background = AsyncZip('mydata.txt', 'myarchive.zip') | |||
|
16 | background.start() | |||
|
17 | print 'The main program continues to run in foreground.' | |||
|
18 | ||||
|
19 | background.join() # Wait for background task to finish | |||
|
20 | print 'Main program waited until background was done.' |
@@ -0,0 +1,69 | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | ||||
|
3 | import subprocess | |||
|
4 | import threading | |||
|
5 | ||||
|
6 | class Pinger(object): | |||
|
7 | status = {'alive': [], 'dead': []} # Populated while we are running | |||
|
8 | hosts = [] # List of all hosts/ips in our input queue | |||
|
9 | ||||
|
10 | # How many ping process at the time. | |||
|
11 | thread_count = 4 | |||
|
12 | ||||
|
13 | # Lock object to keep track the threads in loops, where it can potentially be race conditions. | |||
|
14 | lock = threading.Lock() | |||
|
15 | ||||
|
16 | def ping(self, ip): | |||
|
17 | # Use the system ping command with count of 1 and wait time of 1. | |||
|
18 | ret = subprocess.call(['ping', '-c', '1', '-W', '1', ip], | |||
|
19 | stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w')) | |||
|
20 | ||||
|
21 | return ret == 0 # Return True if our ping command succeeds | |||
|
22 | ||||
|
23 | def pop_queue(self): | |||
|
24 | ip = None | |||
|
25 | ||||
|
26 | self.lock.acquire() # Grab or wait+grab the lock. | |||
|
27 | ||||
|
28 | if self.hosts: | |||
|
29 | ip = self.hosts.pop() | |||
|
30 | ||||
|
31 | self.lock.release() # Release the lock, so another thread could grab it. | |||
|
32 | ||||
|
33 | return ip | |||
|
34 | ||||
|
35 | def dequeue(self): | |||
|
36 | while True: | |||
|
37 | ip = self.pop_queue() | |||
|
38 | ||||
|
39 | if not ip: | |||
|
40 | return None | |||
|
41 | ||||
|
42 | result = 'alive' if self.ping(ip) else 'dead' | |||
|
43 | self.status[result].append(ip) | |||
|
44 | ||||
|
45 | def start(self): | |||
|
46 | threads = [] | |||
|
47 | ||||
|
48 | for i in range(self.thread_count): | |||
|
49 | # Create self.thread_count number of threads that together will | |||
|
50 | # cooperate removing every ip in the list. Each thread will do the | |||
|
51 | # job as fast as it can. | |||
|
52 | t = threading.Thread(target=self.dequeue) | |||
|
53 | t.start() | |||
|
54 | threads.append(t) | |||
|
55 | ||||
|
56 | # Wait until all the threads are done. .join() is blocking. | |||
|
57 | [ t.join() for t in threads ] | |||
|
58 | ||||
|
59 | return self.status | |||
|
60 | ||||
|
61 | if __name__ == '__main__': | |||
|
62 | ping = Pinger() | |||
|
63 | ping.thread_count = 8 | |||
|
64 | ping.hosts = [ | |||
|
65 | '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', | |||
|
66 | 'google.com', 'github.com', 'nonexisting', '127.0.1.2', '*not able to ping!*', '8.8.8.8' | |||
|
67 | ] | |||
|
68 | ||||
|
69 | print ping.start() No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now