Skip to content

Commit c54321b

Browse files
committed
Supports multi-threaded uploading
1 parent cf24e69 commit c54321b

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ InstallationTools/build/
1212
ReleaseTools/Zip/
1313
InstallationTools/Download/
1414
InstallationTools/BANDIZIP-PORTABLE/Bandizip.x64.ini
15+
ReleaseTools/__pycache__

ReleaseTools/Release.exe

1.62 KB
Binary file not shown.

ReleaseTools/Release.py

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
from dotenv import load_dotenv
88
import winreg
99
import locale
10+
import threading
11+
import time
12+
from concurrent.futures import ThreadPoolExecutor
1013

1114

1215
# Inputs: MooaRootDir engineBranchName projectBranchName [--Clean --BuildEngine --ZipEngine --ZipProject --Release --Reupload]
1316

1417
# ================= Defines =================
1518
repo_name = "JasonMa0012/MooaToon"
1619

17-
mooatoon_root_path = r"E:\MooaToon"
20+
mooatoon_root_path = r"E:\Mooa"
1821
if len(sys.argv) > 1:
1922
mooatoon_root_path = sys.argv[1]
2023

@@ -28,6 +31,7 @@
2831

2932
# Default Input
3033
argv = [
34+
# '--Release'
3135
'--Reupload'
3236
]
3337
if len(sys.argv) > 4:
@@ -87,6 +91,68 @@ def async_run(args):
8791
return process.poll()
8892

8993

94+
def upload_single_asset(repo_name, tag_name, file_path, lock):
95+
"""上传单个文件的线程函数,失败时无限重试"""
96+
file_name = os.path.basename(file_path)
97+
retry_count = 0
98+
99+
with lock:
100+
print(f"开始上传: {file_name}")
101+
102+
while True:
103+
try:
104+
ghr.gh_asset_upload(repo_name, tag_name, [file_path])
105+
106+
with lock:
107+
if retry_count > 0:
108+
print(f"上传成功: {file_name} (重试 {retry_count} 次后成功)")
109+
else:
110+
print(f"上传成功: {file_name}")
111+
return True
112+
113+
except Exception as e:
114+
retry_count += 1
115+
with lock:
116+
print(f"上传失败: {file_name}, 错误: {str(e)}")
117+
print(f"第 {retry_count} 次重试中... (3秒后重试)")
118+
119+
# 等待3秒后重试
120+
time.sleep(3)
121+
122+
123+
def multithread_upload(repo_name, tag_name, file_paths):
124+
"""多线程上传文件,每个文件都会重试直到成功"""
125+
if not file_paths:
126+
print("没有文件需要上传")
127+
return True
128+
129+
# 计算并发线程数:文件总数的1/3,但至少1个
130+
max_workers = max(1, len(file_paths) // 3)
131+
132+
print(f"开始多线程上传 {len(file_paths)} 个文件...")
133+
print(f"同时上传文件数量: {max_workers} (文件总数的1/3)")
134+
print("注意: 每个文件都会重试直到上传成功")
135+
136+
lock = threading.Lock()
137+
138+
# 使用线程池控制并发数量
139+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
140+
# 提交所有上传任务
141+
futures = []
142+
for file_path in file_paths:
143+
future = executor.submit(upload_single_asset, repo_name, tag_name, file_path, lock)
144+
futures.append(future)
145+
146+
# 等待所有任务完成
147+
for future in futures:
148+
future.result() # 这会等待任务完成,如果有异常会抛出
149+
150+
with lock:
151+
print(f"所有文件上传完成: {len(file_paths)} 个文件全部上传成功")
152+
153+
return True # 现在总是返回True,因为会无限重试直到成功
154+
155+
90156
# ================= Main ==================
91157
load_dotenv(dotenv_path=get_onedrive_env_path())
92158

@@ -145,21 +211,29 @@ def async_run(args):
145211
publish=False,
146212
body=comment,
147213
name=release_name,
148-
asset_pattern=file_paths,
149214
)
150-
ghr.gh_release_publish(repo_name, release_name)
215+
# 使用多线程上传
216+
upload_success = multithread_upload(repo_name, release_name, file_paths)
217+
if upload_success:
218+
ghr.gh_release_publish(repo_name, release_name)
219+
else:
220+
print("部分文件上传失败,请检查后重试")
151221

152222
if '--Reupload' in argv:
153223
print("======Reupload======")
154224
if last_draft_info is not None:
155225
# 仅上传失败的文件
156226
for asset in last_draft_info['assets']:
157-
for file_path in file_paths:
227+
for file_path in file_paths[:]: # 使用副本避免在迭代时修改列表
158228
if file_path.endswith(asset['name']):
159229
file_paths.remove(file_path)
160230

161-
ghr.gh_asset_upload(repo_name, last_draft_info['tag_name'], file_paths)
162-
ghr.gh_release_publish(repo_name, last_draft_info['tag_name'])
231+
# 使用多线程上传
232+
upload_success = multithread_upload(repo_name, last_draft_info['tag_name'], file_paths)
233+
if upload_success:
234+
ghr.gh_release_publish(repo_name, last_draft_info['tag_name'])
235+
else:
236+
print("部分文件上传失败,请检查后重试")
163237
else:
164238
print("\nThere is no draft!\n")
165239

0 commit comments

Comments
 (0)