Last active
May 30, 2024 08:54
-
-
Save SerhoLiu/a3d7be43df882af80ef98bc375fc6046 to your computer and use it in GitHub Desktop.
Python multiprocessing safe RotatingFileHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
from logging.handlers import RotatingFileHandler | |
class SafeRotatingFileHandler(RotatingFileHandler): | |
""" | |
多进程下 RotatingFileHandler 会出现问题 | |
""" | |
_rollover_lock = multiprocessing.Lock() | |
def emit(self, record): | |
""" | |
Emit a record. | |
Output the record to the file, catering for rollover as described | |
in doRollover(). | |
""" | |
try: | |
if self.shouldRollover(record): | |
with self._rollover_lock: | |
if self.shouldRollover(record): | |
self.doRollover() | |
logging.FileHandler.emit(self, record) | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except: | |
self.handleError(record) | |
def shouldRollover(self, record): | |
if self._should_rollover(): | |
# if some other process already did the rollover we might | |
# checked log.1, so we reopen the stream and check again on | |
# the right log file | |
if self.stream: | |
self.stream.close() | |
self.stream = self._open() | |
return self._should_rollover() | |
return 0 | |
def _should_rollover(self): | |
if self.maxBytes > 0: | |
self.stream.seek(0, 2) | |
if self.stream.tell() >= self.maxBytes: | |
return True | |
return False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment