欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3 flask 文件占用未釋放問題

 更新時(shí)間:2023年11月07日 10:50:40   作者:太陽曬PP  
這篇文章主要介紹了python3 flask 文件占用未釋放問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

刪除文件報(bào)錯(cuò)文件被進(jìn)程占用

    starttime = datetime.datetime.now()
    files = os.listdir(os.getcwd())
    if 'scan.log' in files:
        os.remove(os.path.join(os.getcwd(), 'scan.log'))
    if 'scanResult.log' in files:
        os.remove(os.path.join(os.getcwd(), 'scanResult.log'))

原因

python logging模塊打日志,句柄未釋放導(dǎo)致,本地test.py調(diào)試不會出問題因?yàn)閜ython執(zhí)行完會退出

解決

在刪除文件前,調(diào)logging.shutdown()

import logging
from logging.handlers import TimedRotatingFileHandler
formatter = logging.Formatter(
    fmt='[%(levelname)s] %(asctime)s  %(message)s',
    datefmt="%Y-%m-%d_%H:%M:%S"
)

def getLogger(name,logPath):
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    if not logger.handlers:
        sh = logging.StreamHandler()
        sh.setLevel(logging.ERROR)
        fh = TimedRotatingFileHandler(logPath, when='midnight', backupCount=30, encoding='utf-8')
        fh.setFormatter(logging.INFO)
        sh.setFormatter(formatter)
        fh.setFormatter(formatter)
        logger.addHandler(sh)
        logger.addHandler(fh)

    return logger

def shutdown():
    logging.shutdown()

排錯(cuò)

懷疑自己寫的with open沒釋放?按道理會自動(dòng)釋放,加了行f.close還是報(bào)錯(cuò)被占用

懷疑上傳文件未及時(shí)釋放,ftp.quit()、ftp.close()、還是不行

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論