David Blume commited on 2016-08-14 11:18:22
Showing 8 changed files, with 121 additions and 95 deletions.
... | ... |
@@ -1,61 +0,0 @@ |
1 |
-# From https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/threading.html |
|
2 |
-import threading, queue |
|
3 |
- |
|
4 |
-############################################################################# |
|
5 |
- |
|
6 |
-counter = 0 |
|
7 |
- |
|
8 |
-counter_queue = queue.Queue() |
|
9 |
- |
|
10 |
-def counter_manager(): |
|
11 |
- 'I have EXCLUSIVE rights to update the counter variable' |
|
12 |
- global counter |
|
13 |
- |
|
14 |
- while True: |
|
15 |
- increment = counter_queue.get() |
|
16 |
- counter += increment |
|
17 |
- print_queue.put([ |
|
18 |
- 'The count is %d' % counter, |
|
19 |
- '---------------']) |
|
20 |
- counter_queue.task_done() |
|
21 |
- |
|
22 |
-t = threading.Thread(target=counter_manager) |
|
23 |
-t.daemon = True |
|
24 |
-t.start() |
|
25 |
-del t |
|
26 |
- |
|
27 |
-############################################################################# |
|
28 |
- |
|
29 |
-print_queue = queue.Queue() |
|
30 |
- |
|
31 |
-def print_manager(): |
|
32 |
- 'I have EXCLUSIVE rights to call the "print" keyword' |
|
33 |
- while True: |
|
34 |
- job = print_queue.get() |
|
35 |
- for line in job: |
|
36 |
- print(line) |
|
37 |
- print_queue.task_done() |
|
38 |
- |
|
39 |
-t = threading.Thread(target=print_manager) |
|
40 |
-t.daemon = True |
|
41 |
-t.start() |
|
42 |
-del t |
|
43 |
- |
|
44 |
-############################################################################# |
|
45 |
- |
|
46 |
-def worker(): |
|
47 |
- 'My job is to increment the counter and print the current count' |
|
48 |
- counter_queue.put(1) |
|
49 |
- |
|
50 |
-print_queue.put(['Starting up']) |
|
51 |
-worker_threads = [] |
|
52 |
-for i in range(10): |
|
53 |
- t = threading.Thread(target=worker) |
|
54 |
- worker_threads.append(t) |
|
55 |
- t.start() |
|
56 |
-for t in worker_threads: |
|
57 |
- t.join() |
|
58 |
- |
|
59 |
-counter_queue.join() |
|
60 |
-print_queue.put(['Finishing up']) |
|
61 |
-print_queue.join() |
... | ... |
@@ -0,0 +1 @@ |
1 |
+__all__ = ["counter", ] |
... | ... |
@@ -0,0 +1,70 @@ |
1 |
+#!/usr/bin/env python |
|
2 |
+# From https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/threading.html |
|
3 |
+from __future__ import print_function |
|
4 |
+import threading, Queue |
|
5 |
+ |
|
6 |
+############################################################################# |
|
7 |
+ |
|
8 |
+_counter = 0 |
|
9 |
+ |
|
10 |
+counter_queue = Queue.Queue() |
|
11 |
+ |
|
12 |
+def counter_manager(): |
|
13 |
+ 'I have EXCLUSIVE rights to update the _counter variable' |
|
14 |
+ global _counter |
|
15 |
+ |
|
16 |
+ while True: |
|
17 |
+ increment = counter_queue.get() |
|
18 |
+ _counter += increment |
|
19 |
+ print_queue.put([ |
|
20 |
+ 'The count is %d' % _counter, |
|
21 |
+ '---------------']) |
|
22 |
+ counter_queue.task_done() |
|
23 |
+ |
|
24 |
+############################################################################# |
|
25 |
+ |
|
26 |
+print_queue = Queue.Queue() |
|
27 |
+ |
|
28 |
+def print_manager(): |
|
29 |
+ 'I have EXCLUSIVE rights to call the "print" keyword' |
|
30 |
+ while True: |
|
31 |
+ job = print_queue.get() |
|
32 |
+ for line in job: |
|
33 |
+ print(line) |
|
34 |
+ print_queue.task_done() |
|
35 |
+ |
|
36 |
+############################################################################# |
|
37 |
+ |
|
38 |
+def worker(): |
|
39 |
+ 'My job is to increment the counter and print the current count' |
|
40 |
+ counter_queue.put(1) |
|
41 |
+ |
|
42 |
+def run(): |
|
43 |
+ # Set up the counter manager |
|
44 |
+ t = threading.Thread(target=counter_manager) |
|
45 |
+ t.daemon = True |
|
46 |
+ t.start() |
|
47 |
+ del t |
|
48 |
+ |
|
49 |
+ # Set up the print manager |
|
50 |
+ t = threading.Thread(target=print_manager) |
|
51 |
+ t.daemon = True |
|
52 |
+ t.start() |
|
53 |
+ del t |
|
54 |
+ |
|
55 |
+ # Start doing work |
|
56 |
+ print_queue.put(['Starting up']) |
|
57 |
+ worker_threads = [] |
|
58 |
+ for i in range(10): |
|
59 |
+ t = threading.Thread(target=worker) |
|
60 |
+ worker_threads.append(t) |
|
61 |
+ t.start() |
|
62 |
+ for t in worker_threads: |
|
63 |
+ t.join() |
|
64 |
+ |
|
65 |
+ counter_queue.join() |
|
66 |
+ print_queue.put(['Finishing up']) |
|
67 |
+ print_queue.join() |
|
68 |
+ |
|
69 |
+if __name__ == '__main__': |
|
70 |
+ run() |
... | ... |
@@ -1,29 +0,0 @@ |
1 |
-# https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/process.html |
|
2 |
-import urllib.request |
|
3 |
- |
|
4 |
-sites = [ |
|
5 |
- 'https://www.yahoo.com/', |
|
6 |
- 'http://www.cnn.com', |
|
7 |
- 'http://www.python.org', |
|
8 |
- 'http://www.jython.org', |
|
9 |
- 'http://www.pypy.org', |
|
10 |
- 'http://www.perl.org', |
|
11 |
- 'http://www.cisco.com', |
|
12 |
- 'http://www.facebook.com', |
|
13 |
- 'http://www.twitter.com', |
|
14 |
- 'http://www.macrumors.com/', |
|
15 |
- 'http://arstechnica.com/', |
|
16 |
- 'http://www.reuters.com/', |
|
17 |
- 'http://abcnews.go.com/', |
|
18 |
- 'http://www.cnbc.com/', |
|
19 |
- 'http://www.cnbc.com/', |
|
20 |
-] |
|
21 |
- |
|
22 |
-def sitesize(url): |
|
23 |
- ''' Determine the size of a website ''' |
|
24 |
- with urllib.request.urlopen(url) as u: |
|
25 |
- page = u.read() |
|
26 |
- return url, len(page) |
|
27 |
- |
|
28 |
-for result in map(sitesize, sites): |
|
29 |
- print(result) |
... | ... |
@@ -0,0 +1 @@ |
1 |
+__all__ = ["sitesize", ] |
... | ... |
@@ -0,0 +1,39 @@ |
1 |
+#!/usr/bin/env python |
|
2 |
+# https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/process.html |
|
3 |
+from __future__ import print_function |
|
4 |
+import urllib |
|
5 |
+ |
|
6 |
+sites = [ |
|
7 |
+# 'https://www.yahoo.com/', |
|
8 |
+# 'http://www.cnn.com', |
|
9 |
+# 'http://www.python.org', |
|
10 |
+# 'http://www.jython.org', |
|
11 |
+ 'http://www.pypy.org', |
|
12 |
+# 'http://www.perl.org', |
|
13 |
+# 'http://www.cisco.com', |
|
14 |
+# 'http://www.facebook.com', |
|
15 |
+# 'http://www.twitter.com', |
|
16 |
+# 'http://www.macrumors.com/', |
|
17 |
+# 'http://arstechnica.com/', |
|
18 |
+# 'http://www.reuters.com/', |
|
19 |
+# 'http://abcnews.go.com/', |
|
20 |
+# 'http://www.cnbc.com/', |
|
21 |
+] |
|
22 |
+ |
|
23 |
+ |
|
24 |
+def sitesize(url): |
|
25 |
+ ''' Determine the size of a website ''' |
|
26 |
+ # TODO: Use requests instead, or migrate to python3 |
|
27 |
+ f = urllib.urlopen(url) # Could use "with" in python3 |
|
28 |
+ page = f.read() |
|
29 |
+ f.close() |
|
30 |
+ return url, len(page) |
|
31 |
+ |
|
32 |
+ |
|
33 |
+def run(): |
|
34 |
+ for result in map(sitesize, sites): |
|
35 |
+ print(result) |
|
36 |
+ |
|
37 |
+ |
|
38 |
+if __name__ == '__main__': |
|
39 |
+ run() |
... | ... |
@@ -2,10 +2,13 @@ |
2 | 2 |
# -*- coding: utf-8 -*- |
3 | 3 |
# |
4 | 4 |
# "Hello Friends" in Chinese: 朋友你好 |
5 |
+from __future__ import print_function |
|
5 | 6 |
import os |
6 | 7 |
import sys |
7 | 8 |
import time |
8 | 9 |
from argparse import ArgumentParser |
10 |
+import counter.counter |
|
11 |
+import sitesize.sitesize |
|
9 | 12 |
|
10 | 13 |
|
11 | 14 |
def set_v_print(verbose): |
... | ... |
@@ -18,7 +21,7 @@ def set_v_print(verbose): |
18 | 21 |
global v_print |
19 | 22 |
if verbose: |
20 | 23 |
def v_print(*s): |
21 |
- print ' '.join([i.encode('utf8') for i in s]) |
|
24 |
+ print(' '.join([i.encode('utf8') for i in s])) |
|
22 | 25 |
else: |
23 | 26 |
v_print = lambda *s: None |
24 | 27 |
|
... | ... |
@@ -26,9 +29,11 @@ def set_v_print(verbose): |
26 | 29 |
def main(debug): |
27 | 30 |
start_time = time.time() |
28 | 31 |
localdir = os.path.abspath(os.path.dirname(sys.argv[0])) |
29 |
- v_print("(verbose mode) debug =", str(debug)) |
|
30 |
- v_print("(verbose mode) debug = %s, localdir = %s" % (repr(debug), localdir)) |
|
31 |
- print "Done. That took %1.2fs." % (time.time() - start_time) |
|
32 |
+ v_print("Running counter...") |
|
33 |
+ counter.counter.run() |
|
34 |
+ v_print("Running sitesize...") |
|
35 |
+ sitesize.sitesize.run() |
|
36 |
+ print("Done. That took %1.2fs." % (time.time() - start_time)) |
|
32 | 37 |
|
33 | 38 |
|
34 | 39 |
if __name__ == '__main__': |
35 | 40 |