Skip to content

Commit 9a5d728

Browse files
committed
fix: thread pool terminate
1 parent 8b2cd71 commit 9a5d728

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

bing_images/bing.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
from crawler import crawl_image_urls
77
except ImportError: # Python 3
88
from .crawler import crawl_image_urls
9-
from typing import Counter, List
9+
from typing import List
1010
from multiprocessing.pool import ThreadPool
1111
from time import time as timer
1212
import os
1313
import math
1414

15+
_FINISH = False
16+
1517

1618
def fetch_image_urls(
1719
query: str,
@@ -86,11 +88,16 @@ def rename_images(dir, prefix):
8688

8789

8890
def download_image_entries(entries, pool_size, limit):
91+
global _FINISH
8992
counter = 1
90-
results = ThreadPool(pool_size).imap_unordered(
93+
_FINISH = False
94+
pool = ThreadPool(pool_size)
95+
results = pool.imap_unordered(
9196
download_image_with_thread, entries)
9297
for (url, result) in results:
9398
if counter > limit:
99+
_FINISH = True
100+
pool.terminate()
94101
break
95102
if result:
96103
print("#{} {} Downloaded".format(counter, url))
@@ -109,6 +116,8 @@ def get_image_entries(urls, dir):
109116

110117

111118
def download_image_with_thread(entry):
119+
if _FINISH:
120+
return
112121
url, path = entry
113122
result = download_image(url, path)
114123
return (url, result)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = bing_images
3-
version = 0.1.0
3+
version = 0.1.1
44
author = CatchZeng
55
author_email = catchzenghh@gmail.com
66
description = Python library to fetch image urls and download using multithreading from Bing.com.

0 commit comments

Comments
 (0)