Qilan2 commited on
Commit
1eac76c
·
verified ·
1 Parent(s): ce09299

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +20 -13
server.py CHANGED
@@ -140,7 +140,11 @@ def ssh():
140
  ssh()
141
  clear_output()
142
 
143
-
 
 
 
 
144
 
145
  # 创建一个锁
146
  lock = threading.Lock()
@@ -153,24 +157,24 @@ def update_last_uploaded_size(size):
153
  global last_uploaded_size
154
  last_uploaded_size = size
155
 
 
 
156
  def compress_folder():
157
- global last_uploaded_size
158
-
159
  # 清理并创建目录 /a
160
  if os.path.exists('/a'):
161
- shutil.rmtree('/a') # 更安全地删除目录
162
-
163
  os.makedirs('/a', exist_ok=True)
164
 
165
- with lock: # 使用锁以防止多线程冲突
166
-
167
  if os.path.exists(zip_file_path):
168
  print(f"压缩文件 {zip_file_path} 已存在,跳过本次压缩。")
169
  return
170
 
171
  print('开始压缩数据')
172
-
173
- result = subprocess.run(['7z', 'a','-mcp=GBK', '-tzip', '-mx=1','-mmt=2', zip_file_path, data_folder])
174
 
175
  if result.returncode != 0:
176
  print("压缩失败,请检查7z命令是否正确")
@@ -181,14 +185,16 @@ def compress_folder():
181
  file_size = os.path.getsize(zip_file_path) # 获取压缩包的大小
182
  print('文件大小:', file_size, '上次文件大小:', last_uploaded_size)
183
 
184
- if file_size < (1024 ** 3):
185
- print("文件小于1GB,不进行上传。")
 
186
  return
187
 
188
  if last_uploaded_size == -1 or file_size > last_uploaded_size:
 
189
  if last_uploaded_size == -1:
190
  print("首次运行,正在执行上传...")
191
- update_last_uploaded_size(file_size)
192
 
193
  with open(metadata_file_path, 'w') as json_fid:
194
  json.dump(kaggle_metadata, json_fid)
@@ -198,7 +204,7 @@ def compress_folder():
198
 
199
  elif file_size > last_uploaded_size:
200
  print(f"新文件 {zip_file_path} 大于上次上传的大小,正在执行上传...")
201
- update_last_uploaded_size(file_size)
202
 
203
  with open(metadata_file_path, 'w') as json_fid:
204
  json.dump(kaggle_metadata, json_fid)
@@ -209,6 +215,7 @@ def compress_folder():
209
  else:
210
  print(f"新文件 {zip_file_path} 不大于上次上传的大小,跳过")
211
 
 
212
  def repeat_task():
213
  print('------打包线程启动-------------')
214
  while True:
 
140
  ssh()
141
  clear_output()
142
 
143
+ import json
144
+ import os
145
+ import subprocess
146
+ import threading
147
+ import time
148
 
149
  # 创建一个锁
150
  lock = threading.Lock()
 
157
  global last_uploaded_size
158
  last_uploaded_size = size
159
 
160
+
161
+
162
  def compress_folder():
163
+ global last_uploaded_size # 声明使用全局变量
164
+
165
  # 清理并创建目录 /a
166
  if os.path.exists('/a'):
167
+ subprocess.run(['rm', '-r', '/a'])
168
+
169
  os.makedirs('/a', exist_ok=True)
170
 
171
+ with lock: # 使用锁
 
172
  if os.path.exists(zip_file_path):
173
  print(f"压缩文件 {zip_file_path} 已存在,跳过本次压缩。")
174
  return
175
 
176
  print('开始压缩数据')
177
+ result = subprocess.run(['7z', 'a', '-mcp=GBK','-tzip', '-mx=1','-mmt=2', zip_file_path, data_folder])
 
178
 
179
  if result.returncode != 0:
180
  print("压缩失败,请检查7z命令是否正确")
 
185
  file_size = os.path.getsize(zip_file_path) # 获取压缩包的大小
186
  print('文件大小:', file_size, '上次文件大小:', last_uploaded_size)
187
 
188
+ # 判断是否大于800MB (800MB = 800 * 1024 * 1024 字节)
189
+ if file_size < (800 * 1024 ** 2):
190
+ print("文件小于800MB,不进行上传。")
191
  return
192
 
193
  if last_uploaded_size == -1 or file_size > last_uploaded_size:
194
+ # 判断是否为首次运行或上次上传大小为0
195
  if last_uploaded_size == -1:
196
  print("首次运行,正在执行上传...")
197
+ update_last_uploaded_size(file_size) # 更新为当前文件大小
198
 
199
  with open(metadata_file_path, 'w') as json_fid:
200
  json.dump(kaggle_metadata, json_fid)
 
204
 
205
  elif file_size > last_uploaded_size:
206
  print(f"新文件 {zip_file_path} 大于上次上传的大小,正在执行上传...")
207
+ update_last_uploaded_size(file_size) # 更新元数据中的最后一次上传大小
208
 
209
  with open(metadata_file_path, 'w') as json_fid:
210
  json.dump(kaggle_metadata, json_fid)
 
215
  else:
216
  print(f"新文件 {zip_file_path} 不大于上次上传的大小,跳过")
217
 
218
+
219
  def repeat_task():
220
  print('------打包线程启动-------------')
221
  while True: