Closed flt6 closed 1 year ago
Hi.
I believe the problem here is you are calling colorama.init
from Log
's constructor, and then instantiating Log
many times. This causes the stdout stream to be wrapped again and again, making the calls more and more recursive and slower, until you get the error.
Here is a more minimal example that may reproduce the same behavior:
from colorama import init
class Log:
def __init__(self) -> None:
init(autoreset=True)
print('Hello')
for i in range(10000):
Log()
The solution would be to call colorama.init
only once, outside the constructor.
The above solution seams to work. Had the same problem, but now it's fixed. Thank you.
Hi. I believe the problem here is you are calling
colorama.init
fromLog
's constructor, and then instantiatingLog
many times. This causes the stdout stream to be wrapped again and again, making the calls more and more recursive and slower, until you get the error. Here is a more minimal example that may reproduce the same behavior:from colorama import init class Log: def __init__(self) -> None: init(autoreset=True) print('Hello') for i in range(10000): Log()
The solution would be to call
colorama.init
only once, outside the constructor.
This is just an example code just for showing the problem. However, I only called the constructor function of Log
for once in the other program, but this error also occurred.
Like this:
class ToServer:
def __init__(self, optDir):
self.logger = getLogger("ToServer") # this is overwritten with my own `Log` class with colorful print
self.logger.debug("Class 'ToServer' init successfully.")
# ---------some other code------
def _download(self, st, retry: set[Chapter], chapters, bar):
for task, j in st:
try:
# -------Some Other Code----------
if ret.reason != consts.TTS_SUC:
self.logger.debug("AsyncReq not success `get`")
self.logger.error(ret.reason)
self.logger.error(ret.cancellation_details)
self.logger.debug(chapters[j].idx)
Hi @flt6 . I can't run your latest snippet to test. Can you try to get a minimal reproducible example? i.e. as few extra lines as possible, but with all required imports/definitions, so it can be run to show the problem, but without having to read unrelated code. The first example had the bug with multiple calls to init. I can't tell what the problem here from the code you posted because the given snippet doesn't reference colorama.
Sorry, I reviewed all my code recently. I found some code called constructor for several times in a third party code.
I found maybe it is impossible to use logging with colorama
for multiprocessing
module which pickle
is used but pickle
is not available directly in logging
. To deal with it, repeating calling constructor is needed ( By calling getLogger
with Logger
name for many times).
I build a example code like this.
After log for many times, an error occurred.
This example means when overwrite
Log
oflogging
to support colorful print, call it for many times, this error occurred.