Skip to content

Commit 524310a

Browse files
committed
Add: max_threads options
1 parent 3531401 commit 524310a

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

comiccrawler/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class Config:
2828
"mission_conflict_action": "update",
2929
"browser": "",
3030
"browser_profile": "",
31-
"max_errors": "10"
31+
"max_errors": "10",
32+
"max_threads": "3"
3233
}
3334
def __init__(self, path):
3435
self.path = expanduser(path)

comiccrawler/download_manager.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(self):
6161
"""Construct."""
6262
self.lock = Lock()
6363
self.crawlers: bidict[ModuleType, Worker] = bidict()
64+
self.max_threads: int = setting.getint("max_threads", 3)
6465
self.mod_errors: dict[ModuleType, int] = defaultdict(int)
6566
self.analyze_threads = ThreadSafeSet()
6667
self.library_thread = None
@@ -130,7 +131,7 @@ def _(event):
130131
self.mod_errors[mod] = 0
131132
return
132133

133-
self.start_download_mod(mod)
134+
self.start_download_all(continue_=True)
134135

135136
@thread.listen("DOWNLOAD_INVALID")
136137
def _(event):
@@ -144,10 +145,11 @@ def start_download(self):
144145
"""Start downloading."""
145146
self.start_download_all()
146147

147-
def start_download_all(self):
148+
def start_download_all(self, continue_=False):
148149
missions = mission_manager.get_all("view", lambda m: m.state in ("ANALYZED", "PAUSE", "ERROR", "UPDATE"))
149150
if not missions:
150-
print("所有任務已下載完成")
151+
if not continue_:
152+
print("所有任務已下載完成")
151153
return
152154

153155
for mission in missions:
@@ -163,6 +165,9 @@ def start_download_mod(self, mod):
163165
if mod in self.crawlers:
164166
return
165167

168+
if len(self.crawlers) >= self.max_threads:
169+
return
170+
166171
def do_download():
167172
debug_log("do_download")
168173
with load_episodes(mission):
@@ -181,7 +186,7 @@ def stop_download(self):
181186

182187
# FIXME: shouldn't we wait for thread stop?
183188
self.crawlers.clear()
184-
print("Stop downloading")
189+
print("Stop downloading, download_manager.crawlers cleared")
185190

186191
def start_analyze(self, mission, on_finished=None):
187192
"""Start analyzing"""

comiccrawler/grabber.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ def _():
207207
content_list.append(chunk)
208208
counter.update(len(chunk))
209209
loaded += len(chunk)
210-
except WorkerExit:
210+
finally:
211+
# FIXME: is it safe to always close the connection?
211212
r.close()
212-
raise
213213
if total and loaded < total:
214214
raise IncompleteRead(loaded, total - loaded)
215215
b = None

comiccrawler/mods/twitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ def variables(s):
235235
)
236236

237237
def errorhandler(err, crawler):
238-
if is_http(err, 404) and crawler.image.url == err.request.url:
239-
if match := re.match("(.*):orig", err.request.url):
238+
if is_http(err, 404) and crawler.image.url == err.response.request.url:
239+
if match := re.match("(.*):orig", err.response.request.url):
240240
crawler.image.url = match.group(1)
241241
return
242242

0 commit comments

Comments
 (0)