Я использую регистратор TimedRotatingFileHandler в моем приложении flask. Когда HTTP-запрос отправляется приложению в первый раз, код выполняется и успешно создает файл журнала APIlog.log. Когда отправляется второй HTTP-запрос, выполняется тот же фрагмент кода, и я получаю исключение об использовании файла журнала другим процессом.
Ниже выдается исключение:
--- Logging error ---
Traceback (most recent call last):
File "C:\Program Files (x86)\Python 3.5\lib\logging\handlers.py", line 72, in emit
self.doRollover()
File "C:\Program Files (x86)\Python 3.5\lib\logging\handlers.py", line 393, in doRollover
self.rotate(self.baseFilename, dfn)
File "C:\Program Files (x86)\Python 3.5\lib\logging\handlers.py", line 113, in rotate
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\ProjectFiles\\ExceptionLogFiles\\APIlog' -> 'C:\\ProjectFiles\\ExceptionLogFiles\\APIlog.2020_02_06.log'
Call stack:
File "C:\Program Files (x86)\Python 3.5\lib\threading.py", line 891, in _bootstrap
self._bootstrap_inner()
File "C:\Program Files (x86)\Python 3.5\lib\threading.py", line 923, in _bootstrap_inner
self.run()
File "C:\Program Files (x86)\Python 3.5\lib\threading.py", line 871, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\socketserver.py", line 628, in process_request_thread
self.finish_request(request, client_address)
File "C:\Program Files (x86)\Python 3.5\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Program Files (x86)\Python 3.5\lib\socketserver.py", line 684, in __init__
self.handle()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\werkzeug\serving.py", line 327, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "C:\Program Files (x86)\Python 3.5\lib\http\server.py", line 415, in handle
self.handle_one_request()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\werkzeug\serving.py", line 362, in handle_one_request
return self.run_wsgi()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\werkzeug\serving.py", line 304, in run_wsgi
execute(self.server.app)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\werkzeug\serving.py", line 294, in execute
for data in application_iter:
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\werkzeug\debug\__init__.py", line 330, in debug_application
app_iter = self.app(environ, start_response)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\plugin\ferc\api\api.py", line 30, in validate
app.logger.debug("New : CID: {}, Period:{}, Year:{}, FileName:{}".format(cid,period,year,filename))
Message: 'New : CID: C00123, Period:Q4, Year:2016, FileName:TEST.doc'
Arguments: ()
127.0.0.1 - - [06/Feb/2020 16:26:34] "POST /validate HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask_restful\__init__.py", line 269, in error_router
return original_handler(e)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\flask\app.py", line 2097, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
Ниже приведен мой код:
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
from flask_jsonpify import jsonify
import subprocess
from subprocess import check_output
from datetime import datetime
import os
import configparser
import adodbapi
import logging
from logging.handlers import TimedRotatingFileHandler
from collections import OrderedDict
from logging.handlers import RotatingFileHandler
app = Flask(__name__)
api = Api(app)
@app.route('/validate', methods=['POST'])
def validate():
try:
date = datetime.now().strftime('%Y_%m_%d_%H-%M-%S')
cid = request.form.get("cid")
period = request.form.get("period")
year = request.form.get("year")
form = request.form.get("form")
filename = request.files.get("file").filename
app.logger.debug("New : CID: {}, Period:{}, Year:{}, FileName:{}".format(cid,period,year,filename))
except Exception as e:
app.logger.exception("validate function: Something went wrong. ", exc_info=True)
return '"INVALID FILE"'
if __name__ == '__main__':
app.debug = True
logFilePath = "C:/ProjectFiles/ExceptionLogFiles/"
logger = logging.getLogger('APIlogger')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-5s %(message)s', datefmt='%m-%d-%y %H:%M:%S')
#fh = TimedRotatingFileHandler(logFilePath + 'APIlog',when='midnight')
fh = TimedRotatingFileHandler(logFilePath + 'APIlog',when = 's', interval = 30, backupCount = 20)
fh.suffix = "%Y_%m_%d.log"
fh.setFormatter(formatter)
app.logger.addHandler(fh)
app.run(host="0.0.0.0", port='80')
Пожалуйста, кто-нибудь может помочь мне решить эту проблему блокировки.