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

Background logging can reduce CPU usage #202

Closed
joo opened this issue Jun 10, 2023 · 2 comments
Closed

Background logging can reduce CPU usage #202

joo opened this issue Jun 10, 2023 · 2 comments

Comments

@joo
Copy link

joo commented Jun 10, 2023

sleep 50 milliseconds when the queue is empty, will reduce CPU usage by 3%.

class Logger:
    LOGGERS = []

    def __init__(self):
        self.run = True
        self.loggers = {
            logger: logging.getLogger(logger) for logger in self.LOGGERS
        }

    def __call__(self, queue) -> None:
        signal_func(SIGINT, self.stop)
        signal_func(SIGTERM, self.stop)

        while self.run:
            try:
                record: LogRecord = queue.get_nowait()
            except Empty:
                time.sleep(0.05) #### can reduce CPU usage
                continue
            logger = self.loggers.get(record.name)
            logger.handle(record)
@ahopkins
Copy link
Member

I thought we added something like that 🤔

If not, can you add a PR?

@zhu
Copy link
Contributor

zhu commented Jun 29, 2023

I think change queue.get_nowait() to queue.get(timeout=0.05) is better.

zhu added a commit to zhu/sanic-ext that referenced this issue Jun 29, 2023
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

3 participants