search

Home  >  Q&A  >  body text

python 2.7记录日志的问题IOError: [Errno 24] Too many open files

伊谢尔伦伊谢尔伦2803 days ago1205

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-18 10:09:15

    fh =logging.FileHandler(BASE_DIR+'/log/'+username+'_'+GetNowTime()+'_'+str(random.random())+'.log')
    
    这个你每一次创建的handler对象有没有调用`fh.close()`来清除此hander的所拥有的资源。
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:09:15

    Too many files are opened. Generally speaking, the default is a maximum of 1024 files.
    You need to increase this value to verify how many files you can execute ulimit -a. Look at the "open files" inside.
    For more information, please refer to: http:// askubuntu.com/question…

    reply
    0
  • 阿神

    阿神2017-04-18 10:09:15

    logger = logging.getLogger('mylogger'+str(time.time()))
    There is a problem with this sentence. If you don't run get_task2 once, you will get a file handle. If you run it too many times, it will of course be exceeded!

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:09:15

    logger = logging.getLogger('mylogger'+str(time.time()))
    

    This way of writing is inherently bad and the potential risks are too high. It is unreasonable to generate a separate log for each request. The Linux system itself also has a limit on the number of files in a directory. If there are too many requests, the upper limit will be reached and an error will occur.

    As for whether the handler is closed or not, it can only be said to be a programming error at best, but having a separate log for each request is completely a wrong way of thinking.

    reply
    0
  • Cancelreply