Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

Limit the number of open threads (hashtag671) #54

Closed vikramsubramanian closed 2 months ago

vikramsubramanian commented 2 months ago

Hello, in the software we are developing, we need to open (and keep open) lots of threads, and therefore we are sometimes reaching the limit of threads we can open, and will receive the classical can't start a new thread error. Examining the opened threads when we reach that limit, to check if there is anything we could reduce, I realized Loguru opens 20 Threads! That should be ok for most of the users, but I need to limit that to, let's say, 10 Threads! I looked at the docs, but couldn't find anything that showed me how I could do that. Is there any way to do that? Or do you have any other suggestions?

vikramsubramanian commented 2 months ago

Hi.

Loguru is adding one thread per handler configured with enqueue=True, that's it.

20 Loguru threads would mean you called .add(..., enqueue=True) 20 times while never removing any of the added handler. Is that possible according to your code? Maybe you fall into [a common pitfall of multiprocessing]( which happens when code is not guarded by if __name__ == "__main__" and cause same code to be executed multiple times?

vikramsubramanian commented 2 months ago

Wow, thanks for the (REALLY) fast reply! That can make sense, yes! I don't think I fell into the pitfall you said, but it is possible that I have 20 handlers! I'll double check both of the statements, as it seems this will solve the problems (or at least bring light to it)! Thanks!