-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathThreadPool.py
executable file
·74 lines (66 loc) · 2.04 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env python
# -*- coding = utf-8 -*-
import thread
import time
class ThreadPool:
def __init__(self, threadcount):
self.lock = thread.allocate_lock()
self.flagwait = 0
self.flagstop = False
self.tasklist = list()
self.count = threadcount
def start(self):
for i in range(self.count):
thread.start_new_thread(self.task, ())
def task(self):
while True:
self.lock.acquire()
if self.flagstop == True:
self.count = self.count - 1
self.lock.release()
break
if len(self.tasklist) == 0:
self.lock.release()
continue
t = self.tasklist.pop()
self.flagwait = self.flagwait + 1
self.lock.release()
#r = t[0](t[1])
try:
if t[0](t[1]):
self.lock.acquire()
t[2][0].append(t[2][1])
self.lock.release()
except:
pass
#print 'task'
self.lock.acquire()
self.flagwait = self.flagwait - 1
self.lock.release()
def addtask(self, function, parameter, results):
self.lock.acquire()
self.tasklist.insert(0, (function, parameter, results))
self.lock.release()
def clear(self):
self.lock.acquire()
self.tasklist = list()
self.lock.release()
def wait(self):
while True:
time.sleep(0.1)
self.lock.acquire()
if (self.flagstop == True or len(self.tasklist) == 0) and self.flagwait == 0:
self.lock.release()
break
self.lock.release()
self.stop()
def stop(self):
self.lock.acquire()
self.flagstop = True
self.lock.release()
while True:
time.sleep(0.01)
self.lock.acquire()
if self.count == 0:
break
self.lock.release()