简体   繁体   English

OSError: [Errno 24] 通过 Django admin 上传 9000+ csv 个文件时打开的文件太多

[英]OSError: [Errno 24] Too many open files when uploading 9000+ csv files through Django admin

I've been struggling to upload csv files containing data to populate the database of my Django project.我一直在努力上传包含数据的 csv 文件以填充我的 Django 项目的数据库。 I am serving my project using Django 3.1 and Gunicorn if that is important.如果这很重要,我正在使用 Django 3.1 和 Gunicorn 为我的项目提供服务。 I've referred to multiple stack overflow posts regarding the same issue, but none of them have resolved this problem.我已经提到了关于同一问题的多个堆栈溢出帖子,但没有一个解决了这个问题。 Just to list out the steps I've taken to solve this problem:只是为了列出我为解决这个问题所采取的步骤:

  • Ran ulimit -n 50000 in terminal to increase the max number of files that can be opened as suggested by this post .按照这篇文章的建议,在终端中ulimit -n 50000以增加可以打开的最大文件数。
  • Used ulimit -n to find the max number of files that can be opened and changed the configurations of this limit in the limits.conf file, as suggested by this post , to changed the system limits.使用ulimit -n找到可以打开的最大文件数,并按照这篇文章的建议在limits.conf文件中更改此限制的配置,以更改系统限制。 I confirmed it was changed by running ulimit -a我通过运行ulimit -a确认它已更改
  • I believed it might be a memory issue and that Linux was limiting the max amount of space available on the heap so I changhed the configuration for that as well.我认为这可能是 memory 问题,而 Linux 限制了堆上可用的最大空间量,因此我也更改了它的配置。 Fortunately, it was not a memory issue since RAM usage appeared to be extremely stable according to my control panel幸运的是,这不是 memory 问题,因为根据我的控制面板,RAM 使用似乎非常稳定

Here is the code related to my issue:这是与我的问题相关的代码:

admin.py管理员.py

class CsvImportForm(forms.Form):
    csv_upload = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

@admin.register(dummyClass)
class dummyClassAdmin(admin.ModelAdmin):
    search_fields = search fields
    list_display = stuff to display in a list
    list_filter = thing that filters objects
    prepopulated_fields = prepopulated field

@csrf_exempt
def get_urls(self):
    urls = super().get_urls()
    new_urls = [path('upload-csv/', self.upload_csv),]
    return new_urls + urls

@csrf_exempt
def upload_epcsv(self, request):

    if request.method == "POST":
        csv_files = request.FILES.getlist('csv_upload')
        errors = []
        for csv_file in csv_files:
            if not(csv_file.name.endswith('.csv') or  csv_file.name.endswith('.xlsx')):
                messages.warning(request, 'The wrong file type was uploaded')
                return HttpResponseRedirect(request.path_info)

            csv_data = None
            if csv_file.name.endswith('.xlsx'):
                csv_data = pd.read_excel(csv_file, sheet_name=0, engine='openpyxl')
            else:
                csv_data = pd.read_csv(csv_file)
            csv_data.fillna("https://www.a.com", inplace=True)
        
            
            *Insert code that parses through dataframe and populates fields in database with data



        url = reverse('admin:index')
        return HttpResponseRedirect(url)

    form = CsvImportForm()
    data = {"form": form}
    return render(request, "admin/csv_upload.html", data)

Here is the traceback of the error:这是错误的回溯:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
  File "/path_of_app/admin.py", line 185, in upload_epcsv
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 116, in FILES
  File "/path_of_venv/lib/python3.8/site-packages/django/http/request.py", line 350, in _load_post_and_files
  File "/path_of_venv/lib/python3.8/site-packages/django/http/request.py", line 310, in parse_file_upload
  File "/path_of_venv/lib/python3.8/site-packages/django/http/multipartparser.py", line 231, in parse
  File "/path_of_venv/lib/python3.8/site-packages/django/core/files/uploadhandler.py", line 140, in new_file
  File "/path_of_venv/lib/python3.8/site-packages/django/core/files/uploadedfile.py", line 61, in __init__
  File "/.pyenv/versions/3.8.3/lib/python3.8/tempfile.py", line 541, in NamedTemporaryFile
  File "/.pyenv/versions/3.8.3/lib/python3.8/tempfile.py", line 250, in _mkstemp_inner
OSError: [Errno 24] Too many open files: '/tmp/tmpgnj0th91.upload.csv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 114, in __call__
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 52, in technical_500_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 328, in get_traceback_html
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1218, in open
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1074, in _opener
OSError: [Errno 24] Too many open files: '/path_of_venv/lib/python3.8/site-packages/django/views/templates/technical_500.html'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 114, in __call__
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 52, in technical_500_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 328, in get_traceback_html
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1218, in open
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1074, in _opener
OSError: [Errno 24] Too many open files: '/path_of_venv/lib/python3.8/site-packages/django/views/templates/technical_500.html'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 114, in __call__
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 52, in technical_500_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 328, in get_traceback_html
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1218, in open
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1074, in _opener
OSError: [Errno 24] Too many open files: '/path_of_venv/django/views/templates/technical_500.html'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 114, in __call__
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 52, in technical_500_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 328, in get_traceback_html
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1218, in open
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1074, in _opener
OSError: [Errno 24] Too many open files: '/path_of_venv/django/views/templates/technical_500.html'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path_of_venv/lib/python3.8/site-packages/gevent/pywsgi.py", line 999, in handle_one_response
  File "/path_of_venv/lib/python3.8/site-packages/geventwebsocket/handler.py", line 87, in run_application
  File "/path_of_venv/lib/python3.8/site-packages/gevent/pywsgi.py", line 945, in run_application
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 130, in get_response
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 52, in technical_500_response
  File "/path_of_venv/lib/python3.8/site-packages/django/views/debug.py", line 328, in get_traceback_html
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1218, in open
  File "/.pyenv/versions/3.8.3/lib/python3.8/pathlib.py", line 1074, in _opener
OSError: [Errno 24] Too many open files: '/path_of_venv/lib/python3.8/site-packages/django/views/templates/technical_500.html'
2022-03-27T17:21:27Z {'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '55555', 'HTTP_HOST': 'mysite.com', (hidden keys: 40)} failed with OSError

Changed the path names for privacy reasons出于隐私原因更改了路径名称

It seems that is problem is pd.read_csv(csv_file) won't close csv_file which type is UploadedFile .似乎问题是pd.read_csv(csv_file)不会关闭类型为UploadedFilecsv_file Those open files never close and leaked as a result.那些打开的文件永远不会关闭并因此泄漏。

You can try to log csv_data.closed to see if it's still open.您可以尝试记录csv_data.closed以查看它是否仍处于打开状态。

Solutions:解决方案:

  1. (Recommend) Use temporary_file_path while reading it, it will close the file after reading. (推荐)读取时使用temporary_file_path ,读取后关闭文件。
  2. Close it by yourself csv_file.close() finally.最后自己关闭csv_file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSError:[Errno 24]打开的文件太多 - OSError: [Errno 24] Too many open files OSError: [Errno 24] 打开的文件太多 - OS Mojave - OSError: [Errno 24] Too many open files - OS Mojave OSError:[Errno 24]太多打开的文件python,ubuntu - OSError: [Errno 24] Too many open files python , ubuntu OSError: [Errno 24] 使用 Nibabel 打开的文件太多 - OSError: [Errno 24] Too many open files using Nibabel slackclient OSError:[Errno 24]打开的文件太多 - slackclient OSError: [Errno 24] Too many open files OSError: [Errno 24] 打开的文件太多; 在 python; 难以调试 - OSError: [Errno 24] Too many open files; in python; difficult to debug Django从一个图像域保存到另一个图像域:OSError:[Errno 24]打开的文件太多 - Django saving from one imagefield to another: OSError: [Errno 24] Too many open files OSError:[Errno 24]从终端调用脚本时打开的文件太多 - OSError: [Errno 24] Too many open files when invoking script from terminal OSError:[Errno 24]在Twisted中使用Reactor.run()时打开的文件过多 - OSError: [Errno 24] Too many open files when using reactor.run() in Twisted OSError: [Errno 24] 在 tensorflow-federated 中训练差分隐私时打开的文件过多 - OSError: [Errno 24] Too many open files when training differential privacy in tensorflow-federated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM