-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapline.py
executable file
·212 lines (162 loc) · 5.24 KB
/
mapline.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# coding=utf-8
# author: lu4nx <[email protected]>
import sys
import time
import logging
import threading
import traceback
from optparse import OptionParser
if sys.version_info.major == 2:
from Queue import Empty, Queue
elif sys.version_info.major == 3:
from queue import Empty, Queue
queue = Queue()
logging.basicConfig(
filename='run.log',
level=logging.DEBUG,
format='%(asctime)s %(threadName)s %(levelname)s %(message)s',
filemode='w'
)
class PrintProgressThread(threading.Thread):
"""进度输出线程,每3秒向日志里输出一次当前进度
:param: line_total: 输入数据的总行数
"""
def __init__(self, line_total):
threading.Thread.__init__(self)
self.total = line_total
def run(self):
while True:
current = queue.qsize()
if current == 0:
logging.info("current: 100%")
break
progress = int(100 - (current / self.total) * 100)
logging.info("Succeeded: %s%%" % progress)
time.sleep(3)
class WorkThread(threading.Thread):
"""从队列中取得函数以及参数并执行"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.task_queue = queue
def run(self):
while True:
try:
func, args = self.task_queue.get()
if func:
func(args)
except Exception:
traceback.print_exc()
finally:
self.task_queue.task_done()
class ThreadPool(object):
"""线程池的具体实现
:param thread_count: 线程池大小(线程数)
"""
def __init__(self, thread_count=10):
try:
self.thread_count = int(thread_count)
except Exception:
raise SystemExit('thread_count not is a number')
self.work_list = []
self.init_threadpool()
def init_threadpool(self):
"""初始化线程池,并启动线程"""
self.work_queue = Queue()
for _ in range(self.thread_count):
t = WorkThread(self.work_queue)
self.work_list.append(t)
t.setDaemon(True)
t.start()
def putjob(self, work_func, func_args=None):
"""把任务放入线程池调动"""
self.work_queue.put((work_func, func_args))
def wait(self):
self.work_queue.join()
def self_test():
"""线程池自测试"""
# 注意必须要指定参数
def test_func(*argv):
"""从队列中取得url并urlopen,然后打印句柄"""
if sys.version_info.major == 2:
from urllib2 import urlopen
else:
from urllib.request import urlopen
while True:
try:
url = q.get(block=False)
try:
sock = urlopen(url)
print(sock)
sock.close()
except Exception:
traceback.print_exc()
continue
finally:
q.task_done()
except Empty:
break
q = Queue()
urls = ['http://www.baidu.com',
'http://www.qq.com'] * 10
for url in urls:
q.put(url)
# (1) 创建线程池
threadpool = ThreadPool(thread_count=10)
# (2) 将任务放入任务队列
for i in range(3):
threadpool.putjob(test_func, func_args=None)
# (3) 等待完成队列里所有任务
threadpool.wait()
def load_module(module_name_string):
if module_name_string.find(".") == -1:
return __import__(module_name_string)
module_name_list = module_name_string.split(".")
module_obj = __import__(
module_name_list[0],
fromlist=module_name_list[1:]
)
return getattr(module_obj, module_name_list[-1])
def work(*argv):
foreach_func = argv[0]
while True:
try:
line = queue.get(block=False)
foreach_func(line)
except Empty:
logging.info('Done')
break
except Exception:
traceback.print_exc()
if __name__ == '__main__':
usage = "usage: %prog [options] input_file module_name"
parser = OptionParser(usage=usage)
parser.add_option("-c", type='int', help=u"线程池大小", default=20)
parser.add_option("--test", help=u"自测", action="store_true")
(options, args) = parser.parse_args()
if options.test:
self_test()
raise SystemExit()
try:
input_file = args[0]
module_name = args[1]
except IndexError:
parser.print_help()
raise SystemExit()
threadpool_size = options.c
logging.info(u"Loading: %s", module_name)
module_obj = load_module(module_name)
with open(input_file) as f:
for line in f:
queue.put(line.strip())
threadpool = ThreadPool(thread_count=threadpool_size)
logging.info("Thread Pool size: %d" % threadpool_size)
print_progress_thread = PrintProgressThread(queue.qsize())
print_progress_thread.start()
for i in range(threadpool_size):
threadpool.putjob(
work,
func_args=getattr(module_obj, "foreach")
)
print_progress_thread.join()
threadpool.wait()