Python多线程上传文件

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值