import concurrent.futures
import requests
import json
from urllib.parse import urlencode
import os
def filelist(directory):
""" 返回文件列表 """
file_paths = []
for root, directories, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
return file_paths
def task(task_id):
# 这里是每个线程的具体任务
try:
print(f'执行上传文件{task_id}\n')
postfiles = {"file": open(task_id, "rb")}
href = 'http://XXX/projectUpFileTest?userid=XXX&objid=8f34a236db744b7c84596cc60d6db36f&otype=&uptype=normal'
r = requests.post(href, files=postfiles)
mgs = json.loads(r.text)
print(mgs)
message = '上传成功' + str(task_id)
print(message)
except Exception as e:
message = task_id + e
return message
def main(tasks, group_size):
"""任务列表和最大并发进程"""
results = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for i in range(0, len(tasks), group_size):
current_tasks = tasks[i:i + group_size]
# 提交线程任务并获取Future对象
future_to_task = {executor.submit(task, task_id): task_id for task_id in current_tasks}
for future in concurrent.futures.as_completed(future_to_task):
task_id = future_to_task[future]
try:
result = future.result() # 获取线程的返回结果
results.append(result)
except Exception as e:
print(f'任务 {task_id} 出错: {str(e)}')
for result in results:
print(result)
if __name__ == '__main__':
filedir = 'C:\\Users\\Administrator\\Dropbox\\PC\\Downloads\\模型文件' # 本地文件路径
tasklist = filelist(filedir)
print(len(tasklist))
main(tasklist, 5)
Python多线程上传文件
最新推荐文章于 2024-05-06 23:09:46 发布