OSError: [Errno 24] Too many open files Gunicorn

I've a Django application running on a server I'm using gunicorn on server this what my gunicorn service file looks like

myapp.service

[Unit]
Description=myapp daemon
Requires=myapp.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/myappApi/
ExecStart=/myappApi/venv/bin/gunicorn \
          --access-logfile - \
          --workers 10 \
          --timeout 0 \
          --bind unix:/run/myapp.sock \
          myappProject.wsgi:application

[Install]
WantedBy=multi-user.target

and this is the error I'm getting

File "/myappApi/views.py", line 293, in CustomHandler
Jan 23 10:48:30 myapp gunicorn[2957207]:     app = CustomApp()
Jan 23 10:48:30 myapp gunicorn[2957207]:   File "app.py", line 22, in __init__
Jan 23 10:48:30 myapp gunicorn[2957207]:     self.logger = start_logger(__file__)
Jan 23 10:48:30 myapp gunicorn[2957207]:   File "utils/loggerUtil.py", line 19, in start_logger
Jan 23 10:48:30 myapp gunicorn[2957207]: OSError: [Errno 24] Too many open files: 'conf/config.json'

above error is raised by this code

from logging.handlers import RotatingFileHandler
import os, logging, time, json

def start_logger(__file__):
    os.makedirs('logs', exist_ok=True)
    fileName = os.path.basename(__file__)
    logdatetime = time.strftime("%d-%m-%Y-%I-%M")
    logFilename = 'logs/' + fileName[:-3] + logdatetime + '.log'
    
    logger = logging.getLogger(__file__)
    ''' Setting the threshold of logger to DEBUG '''
    with open("conf/config.json") as f1:
        data = json.load(f1)
    # Rotating handler
    handler = RotatingFileHandler(
        logFilename, maxBytes=data['maxBytes'], backupCount=data['backupCount'])
    logger.addHandler(handler)
    return logger

I've checked this answer it increases ulimit of a file which is okay but It's a temporary solution. I've noticed I'm using with context manager so outside of that block files should be closed why it's still open?

Вернуться на верх