-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy paththreadpool.py
56 lines (48 loc) · 1.39 KB
/
threadpool.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
#!/usr/bin/env python
# coding=utf-8
import threading
from Queue import Queue
from Queue import Empty
class ThreadPoolSlot(threading.Thread):
def __init__(self, tasks):
threading.Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.idle = True
self.start()
def run(self):
while True:
try:
func, args, kwargs = self.tasks.get(True, timeout=60)
except Empty, e:
break
self.idle = False
func(*args, **kwargs)
self.idle = True
self.tasks.task_done()
class ThreadPool(object):
def __init__(self, thread_num):
self.tasks = Queue()
self.pool = []
self.threadLock = threading.Lock()
self.__init__thread_pool(thread_num)
def __init__thread_pool(self, thread_num):
for i in range(thread_num):
self.pool.append(ThreadPoolSlot(self.tasks))
def spawn(self, func, *args, **kwargs):
"""
向队列中添加任务
"""
self.tasks.put((func, args, kwargs))
def joinall(self):
self.tasks.join()
for thread in self.pool:
thread.join(5)
def undone_tasks(self):
r = 0
for thread in self.pool:
if not thread.idle:
r += 1
if r:
r += self.tasks.qsize()
return r