Skip to content

Commit

Permalink
modify thread not to block
Browse files Browse the repository at this point in the history
  • Loading branch information
dt3310321 committed Nov 28, 2017
1 parent 89b4eab commit 6ae8f7f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion qcloud_cos/cos_threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self, task_queue, *args, **kwargs):
def run(self):
while True:
func, args, kwargs = self._task_queue.get()

# 判断线程是否需要退出
if func is None:
return
try:
ret = func(*args, **kwargs)
self._succ_task_num += 1
Expand Down Expand Up @@ -53,6 +55,7 @@ def add_task(self, func, *args, **kwargs):
if not self._active:
with self._lock:
if not self._active:
self._workers = []
self._active = True

for i in range(self._num_threads):
Expand All @@ -65,6 +68,11 @@ def add_task(self, func, *args, **kwargs):
def wait_completion(self):
self._queue.join()
self._finished = True
# 已经结束的任务, 需要将线程都退出, 防止卡死
for i in range(self._num_threads):
self._queue.put((None, None, None))

self._active = False

def get_result(self):
assert self._finished
Expand Down

0 comments on commit 6ae8f7f

Please sign in to comment.