如何在Asyncio中使用Python的内置日志记录(权限错误) [英] How to use Python's built-in Logging with Asyncio (Permission Error)

查看:335
本文介绍了如何在Asyncio中使用Python的内置日志记录(权限错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每天晚上都使用TimedRotatingFileHandler从日志记录到新文件.根据记录文档:

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]进程无法访问文件 因为它正在被另一个进程使用: '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'

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?

这是我的客户.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())

推荐答案

我认为与asyncio没有任何关系.您已经启动了多个流程来处理您的工作量.在Windows下,文件被其他进程打开时无法重命名.通常,即使在POSIX下,也不能保证从多个进程写入同一文件不会按预期工作,因为进程没有机制来序列化对文件的访问.因此,答案是要有一个单独的工作进程来写入文件,而其他进程则通过套接字或multiprocessing队列将事件传递给该文件.请参见记录食谱以获取更多信息.

I don't think it has anything to do with asyncio. You've started multiple processes to handle your workload. Under Windows, a file can't be renamed when it is opened by another process. In general, and even under POSIX, writing to the same file from multiple processes isn't guaranteed to work as expected because the processes have no mechanism to serialise access to the file. So the answer is to have a separate worker process that writes to the file, with others communicating events to it via socket or multiprocessing Queue. See the logging cookbook for more information.

这篇关于如何在Asyncio中使用Python的内置日志记录(权限错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆