Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Commit adcad9b

Browse files
committed
Implement multithreading for improved performance
1 parent f671b4f commit adcad9b

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

Diff for: sync/core/Sync.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import concurrent.futures
12
import subprocess
3+
from concurrent.futures import ThreadPoolExecutor
24
from datetime import datetime
35

46
from .Config import Config
@@ -133,12 +135,17 @@ def update(self, module_ids=None, force=False, **kwargs):
133135
else:
134136
tracks = self._tracks.get_tracks(module_ids)
135137

136-
for track in tracks:
137-
online_module = self._update_jsons(track=track, force=force)
138-
if online_module is None:
139-
continue
138+
with ThreadPoolExecutor() as executor:
139+
futures = []
140+
for track in tracks:
141+
futures.append(
142+
executor.submit(self._update_jsons, track=track, force=force)
143+
)
140144

141-
self._log.i(f"update: [{track.id}] -> update to {online_module.version_display}")
145+
for future in concurrent.futures.as_completed(futures):
146+
online_module = future.result()
147+
if online_module is not None:
148+
self._log.i(f"update: [{online_module.id}] -> update to {online_module.version_display}")
142149

143150
def push_by_git(self, branch):
144151
json_file = self._json_folder.joinpath(ModulesJson.filename())

Diff for: sync/track/GithubTracks.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import concurrent.futures
2+
from concurrent.futures import ThreadPoolExecutor
3+
14
import requests
25
from github import Github, Auth, UnknownObjectException
36
from github.Repository import Repository
@@ -80,24 +83,30 @@ def get_tracks(self, user_name, repo_names=None, *, cover=False, use_ssh=True):
8083
self._log.i(f"get_tracks: user_name = {user_name}")
8184

8285
user = self._github.get_user(user_name)
83-
if repo_names is None:
84-
for repo in user.get_repos():
85-
track_json = self._get_from_repo(repo, cover, use_ssh)
86-
if track_json is not None:
87-
self._tracks.append(track_json)
88-
else:
86+
repos = []
87+
88+
if repo_names is not None:
8989
for repo_name in repo_names:
9090
try:
9191
repo = user.get_repo(repo_name)
92+
repos.append(repo)
9293
except UnknownObjectException as err:
93-
repo = None
9494
msg = Log.get_msg(err)
9595
self._log.e(f"get_tracks: [{repo_name}] -> {msg}")
96+
else:
97+
repos = user.get_repos()
98+
99+
with ThreadPoolExecutor() as executor:
100+
futures = []
101+
for repo in repos:
102+
futures.append(
103+
executor.submit(self._get_from_repo, repo=repo, cover=cover, use_ssh=use_ssh)
104+
)
96105

97-
if repo is not None:
98-
track_json = self._get_from_repo(repo, cover, use_ssh)
99-
if track_json is not None:
100-
self._tracks.append(track_json)
106+
for future in concurrent.futures.as_completed(futures):
107+
track_json = future.result()
108+
if track_json is not None:
109+
self._tracks.append(track_json)
101110

102111
self._log.i(f"get_tracks: size = {self.size}")
103112
return self._tracks

0 commit comments

Comments
 (0)