Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous file sink #1049

Open
luyiming opened this issue Dec 23, 2023 · 1 comment
Open

Asynchronous file sink #1049

luyiming opened this issue Dec 23, 2023 · 1 comment

Comments

@luyiming
Copy link

If I understand correctly, we can use enqueue=True on file sinks so that code does not block on logger.info, there is a background thread collecting log records and sending them to file sinks. However, a background thread (with file IO operations) would still block the execution of the main thread. A better way might be to implement an asynchronous file sink using something like aiofile. Then the IO operations can be asynchronously carried out at the operating system level.

Is there any plan on implementing an asynchronous file sink?

@Delgan
Copy link
Owner

Delgan commented Dec 27, 2023

Hi @luyiming.

You're correct about enqueue=True.

Loguru is already compatible with aiofile though, since it supports asynchronous sinks.

from loguru import logger

import asyncio
from pathlib import Path
from tempfile import gettempdir

from aiofile import async_open

tmp_filename = Path("hello.txt")
logger.remove()

async def sink(message):
    async with async_open(tmp_filename, "a") as afp:
        await afp.write(message)
        afp.seek(0)

async def main():
    logger.remove()
    logger.add(sink)

    logger.info("A")
    logger.info("B")
    await logger.complete()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

I would consider improving support for "asynchronous file sink" out-of-the-box if asynchronous file-IO was part of the Python standard library. Right now, I don't want to add a dependency on a third-party library like aiofile. However, creating and sharing such handler in a separate library might prove useful to others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants