吴sir-线程池

线程池

 

版本一:

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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import Queue
import threading
 
 
class ThreadPool(object):
 
def __init__(self, max_num=20):
self.queue = Queue.Queue(max_num)
for i in xrange(max_num):
self.queue.put(threading.Thread)
 
def get_thread(self):
return self.queue.get()
 
def add_thread(self):
self.queue.put(threading.Thread)
 
"""
pool = ThreadPool(10)
 
def func(arg, p):
print arg
import time
time.sleep(2)
p.add_thread()
 
 
for i in xrange(30):
thread = pool.get_thread()
t = thread(target=func, args=(i, pool))
t.start()
"""

  

版本二:

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
from Queue import Queue
import contextlib
import threading
 
WorkerStop = object()
 
 
class ThreadPool:
 
workers = 0
 
threadFactory = threading.Thread
currentThread = staticmethod(threading.currentThread)
 
def __init__(self, maxthreads=20, name=None):
 
self.q = Queue(0)
self.max = maxthreads
self.name = name
self.waiters = []
self.working = []
 
def start(self):
needsiZe = self.q.qsize()
while self.workers < min(self.max, needSize):
self.startAWorker()
 
def startAWorker(self):
self.workers += 1
name = "PoolThread-%s-%s" % (self.name or id(self), self.workers)
newThread = self.threadFactory(target=self._worker, name=name)
newThread.start()
 
def callInThread(self, func, *args, **kw):
self.callInThreadWithCallback(None, func, *args, **kw)
 
def callInThreadWithCallback(self, onResult, func, *args, **kw):
o = (func, args, kw, onResult)
self.q.put(o)
 
 
@contextlib.contextmanager
def _workerState(self, stateList, workerThread):
stateList.append(workerThread)
try:
yield
finally:
stateList.remove(workerThread)
 
def _worker(self):
ct = self.currentThread()
o = self.q.get()
while o is not WorkerStop:
with self._workerState(self.working, ct):
function, args, kwargs, onResult = o
del o
try:
result = function(*args, **kwargs)
success = True
except:
success = False
if onResult is None:
pass
 
else:
pass
 
del function, args, kwargs
 
if onResult is not None:
try:
onResult(success, result)
except:
#context.call(ctx, log.err)
pass
 
del onResult, result
 
with self._workerState(self.waiters, ct):
o = self.q.get()
 
def stop(self):
while self.workers:
self.q.put(WorkerStop)
self.workers -= 1
 
 
"""
def show(arg):
import time
time.sleep(1)
print arg
 
 
pool = ThreadPool(20)
 
for i in range(500):
pool.callInThread(show, i)
 
pool.start()
pool.stop()
"""

  

更多参见:twisted.python.threadpool

上下文管理:https://docs.python.org/2/library/contextlib.html

posted on 2016-01-19 16:45  cwm_kylin  阅读(111)  评论(0)    收藏  举报

导航