I'm using a TimedRotatingFileHandler from logging to log to a new file each night. According to the logging docs:
The system will save old log files by appending extensions to the filename.
And it is when this happens that I get a Permission Error:
--- Logging error ---
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\lh\PythonIntegration\Connect\Logs\WS_API_integration_client' -> 'C:\Users\lh\PythonIntegration\Connect\Logs\WS_API_integration_client.2018-10-08_13-00'
I'm guessing that this has to do with the fact that I have a loop where I run asynchronous processes. But even when I tested it with only one logging event I get the permission error. Which means it's trying to change the extension of the same file it's writing to - hence the permission error. How do I tell logger to close the file so that it can add the extension to the filename?
This is my client.py
rotating_logger = logging.getLogger('ClientLogger')
rotating_logger.setLevel(logging.DEBUG)
handler = logging.handlers.TimedRotatingFileHandler(
log_file, when = 'midnight',backupCount=30)
formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
handler.setFormatter(formatter)
rotating_logger.addHandler(handler)
async def keep_alive(websocket):
"""
Sends keep-alive message to the server.
"""
while websocket.open:
await websocket.send('hello')
await asyncio.sleep(60*1)
async def open_connection():
loop = asyncio.get_event_loop()
with concurrent.futures.ProcessPoolExecutor() as pool:
async with websockets.connect(
'wss://{}:{}@host.net/api/ws'.format(user,pswd),
ssl=True,
max_queue = 1000) as websocket:
"""
Keep connection alive.
"""
asyncio.ensure_future(keep_alive(websocket))
"""
Handle messages from server
"""
while True:
"""
Handle message from server.
"""
message = await websocket.recv()
if message.isdigit():
rotating_logger.info ('Keep alive message: {}'.format(str(message)))
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(open_connection())
from How to use Python's built-in Logging with Asyncio (Permission Error)
No comments:
Post a Comment