如何使用Python请求模块上传一堆文件



当我使用requests模块通过一个请求上传至少400个文件时,我在macOS系统上收到了Too many open files错误。

我试过ulimit -n 20000

还检查了:

  • sysctl kern.maxfiles得到98304
  • sysctl kern.maxfilesperproc得到49152

但是,它不起作用。

这是我的代码:

import os
import requests

url = 'http://127.0.0.1:8000/api/upload'
file_path = '/Users/BonJu/Projects/downloads'
file_list = os.listdir(file_path)
files = []
for file in file_list:
try:
source = open(os.path.join(file_path, file), 'rb')
files.append(('file', source))
except Exception as e:
print('File: %s, Error: %s' % (file, e.__str__()))
continue
response = requests.post(url=url, data={'uploader': 'admin'}, files=files)

终端结果:

File: test_252.docx, Error: [Errno 24] Too many open files: '/Users/BonJu/Projects/downloads/test_252.docx'
File: test_253.docx, Error: [Errno 24] Too many open files: '/Users/BonJu/Projects/downloads/test_253.docx'
File: test_254.docx, Error: [Errno 24] Too many open files: '/Users/BonJu/Projects/downloads/test_254.docx'
...
File: test_418.docx, Error: [Errno 24] Too many open files: '/Users/BonJu/Projects/downloads/test_418.docx'

因为它是一个API服务器,它将发送的日志文件链接到问题,所以我需要在一个请求中发送所有文件,否则经理将收到多封邮件,无法解决问题。

这种情况有什么解决办法吗?

解决方案

最后,我调整了API以保存一个用于存储上传日志的临时文件,并传递一个status参数来控制最终输出。

我的代码:

payload = {
'status': 'finish',
'uploader': 'admin'
}
response = requests.post(url=url, data=payload, files=files)

API:

@api_view(['post'])
def upload(request, debug, api_version):
status = request.POST.get('status')
file_list = request.FILES.getlist('file')
if status == 'finish':
# open the temp file and insert the last logs then output

else:
# create/insert the logs and save to a temp file
return Response({'status': status, 'files': file_list})

我建议用所有这些创建一个单独的tar文件。

或者在读取文件内容后关闭文件(而不是在发布请求之前打开文件描述符

最新更新