I am writing a basic Flask application to accept GET requests. It's not relevant how the GET requests are handled, just that everything is happening in one Python process, and that there's just one function which is routed to handle GET requests.
Suppose I want to log the parameters of the GET request. I want to be able to safely log to the same file in multiple "threads" created from concurrent GET requests. I have read that Python's logging is thread-safe, but I do not understand the conditions in which this is true.
For example, is it safe to log to the same file using different handlers/logger instances without using locks or anything like that? Is it safe to log to the same file from different threads using the same file handler but different logger instances?
I did not try anything yet because concurrency issues are inconsistent and I didn't want to rely on testing it as a means for ensuring its safety
Each
logging.Handlerinstance has its ownLockthat ensures thread-safe usage.So if you create a single
logging.Handler, you can add it to as many loggers as you like, and use it from as many threads as you like, and access to the underlying file will be completely safe.Creating multiple
logging.Handlerinstances pointing to the same file is not thread-safe, however.