@@ -0,0 +1,12 | |||||
|
1 | import threading | |||
|
No newline at end of file | ||||
|
2 | import datetime | |||
|
No newline at end of file | ||||
|
3 | ||||
|
No newline at end of file | ||||
|
4 | class ThreadClass(threading.Thread): | |||
|
No newline at end of file | ||||
|
5 | ||||
|
No newline at end of file | ||||
|
6 | def run(self): | |||
|
No newline at end of file | ||||
|
7 | now = datetime.datetime.now() | |||
|
No newline at end of file | ||||
|
8 | print "%s says Hello World at time: %s" % (self.getName(), now) | |||
|
No newline at end of file | ||||
|
9 | ||||
|
No newline at end of file | ||||
|
10 | for i in range(2): | |||
|
No newline at end of file | ||||
|
11 | t = ThreadClass() | |||
|
No newline at end of file | ||||
|
12 | t.start() No newline at end of file |
@@ -0,0 +1,13 | |||||
|
1 | import urllib2 | |||
|
No newline at end of file | ||||
|
2 | import time | |||
|
No newline at end of file | ||||
|
3 | ||||
|
No newline at end of file | ||||
|
4 | hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", | |||
|
No newline at end of file | ||||
|
5 | "http://ibm.com", "http://apple.com"] | |||
|
No newline at end of file | ||||
|
6 | ||||
|
No newline at end of file | ||||
|
7 | start = time.time() | |||
|
No newline at end of file | ||||
|
8 | #grabs urls of hosts and prints first 1024 bytes of page | |||
|
No newline at end of file | ||||
|
9 | for host in hosts: | |||
|
No newline at end of file | ||||
|
10 | url = urllib2.urlopen(host) | |||
|
No newline at end of file | ||||
|
11 | print url.read(1024) | |||
|
No newline at end of file | ||||
|
12 | ||||
|
No newline at end of file | ||||
|
13 | print "Elapsed Time: %s" % (time.time() - start) No newline at end of file |
@@ -0,0 +1,44 | |||||
|
1 | #!/usr/bin/env python | |||
|
No newline at end of file | ||||
|
2 | import Queue | |||
|
No newline at end of file | ||||
|
3 | import threading | |||
|
No newline at end of file | ||||
|
4 | import urllib2 | |||
|
No newline at end of file | ||||
|
5 | import time | |||
|
No newline at end of file | ||||
|
6 | ||||
|
No newline at end of file | ||||
|
7 | hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", | |||
|
No newline at end of file | ||||
|
8 | "http://ibm.com", "http://apple.com"] | |||
|
No newline at end of file | ||||
|
9 | ||||
|
No newline at end of file | ||||
|
10 | queue = Queue.Queue() | |||
|
No newline at end of file | ||||
|
11 | ||||
|
No newline at end of file | ||||
|
12 | class ThreadUrl(threading.Thread): | |||
|
No newline at end of file | ||||
|
13 | """Threaded Url Grab""" | |||
|
No newline at end of file | ||||
|
14 | def __init__(self, queue): | |||
|
No newline at end of file | ||||
|
15 | threading.Thread.__init__(self) | |||
|
No newline at end of file | ||||
|
16 | self.queue = queue | |||
|
No newline at end of file | ||||
|
17 | ||||
|
No newline at end of file | ||||
|
18 | def run(self): | |||
|
No newline at end of file | ||||
|
19 | while True: | |||
|
No newline at end of file | ||||
|
20 | #grabs host from queue | |||
|
No newline at end of file | ||||
|
21 | host = self.queue.get() | |||
|
No newline at end of file | ||||
|
22 | ||||
|
No newline at end of file | ||||
|
23 | #grabs urls of hosts and prints first 1024 bytes of page | |||
|
No newline at end of file | ||||
|
24 | url = urllib2.urlopen(host) | |||
|
No newline at end of file | ||||
|
25 | print url.read(1024) | |||
|
No newline at end of file | ||||
|
26 | ||||
|
No newline at end of file | ||||
|
27 | #signals to queue job is done | |||
|
No newline at end of file | ||||
|
28 | self.queue.task_done() | |||
|
No newline at end of file | ||||
|
29 | ||||
|
No newline at end of file | ||||
|
30 | start = time.time() | |||
|
No newline at end of file | ||||
|
31 | ||||
|
No newline at end of file | ||||
|
32 | def main(): | |||
|
No newline at end of file | ||||
|
33 | #spawn a pool of threads, and pass them queue instance | |||
|
No newline at end of file | ||||
|
34 | for i in range(5): | |||
|
No newline at end of file | ||||
|
35 | t = ThreadUrl(queue) | |||
|
No newline at end of file | ||||
|
36 | t.setDaemon(True) | |||
|
No newline at end of file | ||||
|
37 | t.start() | |||
|
No newline at end of file | ||||
|
38 | #populate queue with data | |||
|
No newline at end of file | ||||
|
39 | for host in hosts: | |||
|
No newline at end of file | ||||
|
40 | queue.put(host) | |||
|
No newline at end of file | ||||
|
41 | queue.join() | |||
|
No newline at end of file | ||||
|
42 | ||||
|
No newline at end of file | ||||
|
43 | main() | |||
|
No newline at end of file | ||||
|
44 | print "Elapsed Time: %s" % (time.time() - start) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now