forked from tomoncle/Python-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool_test.py
More file actions
72 lines (58 loc) · 1.78 KB
/
thread_pool_test.py
File metadata and controls
72 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/4/29 14:46
# @Author : TOM
# @Site : 模拟线程池
# @File : thread_pool_test.py
# @Software: PyCharm
import time
import threading
import Queue
import urllib2
class Consumer(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self._queue = queue
def run(self):
while True:
content = self._queue.get()
print content
response = urllib2.urlopen(content)
self._queue.task_done()
print content,'_______________ok'
def build_worker_pool(queue, size):
workers = []
for _ in range(size):
worker = Consumer(queue)
# 通过setDaemon(true)来设置线程为“守护线程”
# 在没有用户线程可服务时会自动离开
worker.setDaemon(True)
worker.start()
workers.append(worker)
return workers
def Producer():
urls = [
'http://www.python.org',
'http://www.python.org/about/',
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
'http://www.python.org/doc/',
'http://www.python.org/download/',
'http://www.python.org/getit/',
'http://www.python.org/community/',
'https://wiki.python.org/moin/',
'http://planet.python.org/',
'https://wiki.python.org/moin/LocalUserGroups',
'http://www.python.org/psf/',
'http://docs.python.org/devguide/',
'http://www.python.org/community/awards/'
# etc..
]
start_time = time.time()
queue = Queue.Queue()
build_worker_pool(queue, 4)
for url in urls:
queue.put(url)
queue.join()
print 'time use :', time.time() - start_time
if __name__ == "__main__":
Producer()