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

python3 flask 文件占用未釋放問題

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

背景

刪除文件報(bào)錯文件被進(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()

排錯

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

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

總結(jié)

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

相關(guān)文章

最新評論