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