6

I'm writing my code using asyncio module with a lot of info/debug logging (logging.FileHandler). I'm concerned that extensive using of logging in asyncio will decrease the performance of my application, because logging is blocking operation.

What is the best solution for that? Didn't find any information on logging overhead.

Maybe using SocketHandler or MemoryHandler will help? For metrics I'm using statsd (which is also blocking operation, but must be very fast), but I'm more concerned with non-blocking logging.

Code example:

@asyncio.coroutine
def creator_worker(self):
    while not self.q.empty():
        with (yield from self.semaphore):
            sample = yield from self.q.get()
            logging.debug('Got new sample, processing')
            # start processing

I'm aware of

import os
os.environ['PYTHONASYNCIODEBUG'] = '1'
import asyncio

But that's not what I'm looking for.

Thank you

2 Answers 2

5

Logging document provided a solution, log through a QueueHandler, then use a QueueListener to handle log record in another thread. https://docs.python.org/3.6/howto/logging-cookbook.html#dealing-with-handlers-that-block

Sign up to request clarification or add additional context in comments.

Comments

3

You may create a custom log handler. The handler will push logging record into a queue. Another thread may read data from the queue and perform actual logging.

As an option you may use SysLogHandler -- it's pretty fast. But, anyway, logging too much reduces performance, you can do nothing with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.