better memory handling

This commit is contained in:
Klemek
2021-04-07 18:41:07 +02:00
parent 45d56a3acb
commit 562fd51c91
3 changed files with 131 additions and 115 deletions
+1
View File
@@ -5,3 +5,4 @@ __pycache__
error_*
*.log
/logs/
.vscode
+14
View File
@@ -58,6 +58,15 @@ class GuildLogs:
self.channels = {}
self.locked = False
def __enter__(self):
return self
def __exit__(self, type, value, tb):
del self.channels
del self.guild
if self.locked:
self.unlock()
def dict(self) -> dict:
return {id: self.channels[id].dict() for id in self.channels}
@@ -77,6 +86,7 @@ class GuildLogs:
def unlock(self):
self.locked = False
current_analysis_lock.acquire()
if self.log_file in current_analysis:
current_analysis.remove(self.log_file)
current_analysis_lock.release()
@@ -111,6 +121,7 @@ class GuildLogs:
await code_message(progress, "Reading saved history (2/4)...")
t0 = datetime.now()
json_data = gzip.decompress(gziped_data)
del gziped_data
logging.info(
f"log {self.guild.id} > gzip decompress in {delta(t0):,}ms"
)
@@ -119,6 +130,7 @@ class GuildLogs:
await code_message(progress, "Reading saved history (3/4)...")
t0 = datetime.now()
channels = json.loads(json_data)
del json_data
logging.info(f"log {self.guild.id} > json parse in {delta(t0):,}ms")
if self.check_cancelled():
return CANCELLED, 0
@@ -267,6 +279,7 @@ class GuildLogs:
)
t0 = datetime.now()
gziped_data = gzip.compress(json_data)
del json_data
logging.info(
f"log {self.guild.id} > gzip in {delta(t0):,}ms -> {real_total_msg / deltas(t0):,.3f} m/s"
)
@@ -279,6 +292,7 @@ class GuildLogs:
t0 = datetime.now()
with open(self.log_file, mode="wb") as f:
f.write(gziped_data)
del gziped_data
logging.info(
f"log {self.guild.id} > saved in {delta(t0):,}ms -> {real_total_msg / deltas(t0):,.3f} m/s"
)
+4 -3
View File
@@ -36,15 +36,16 @@ class Scanner(ABC):
):
args = list(args)
guild = message.guild
logs = GuildLogs(guild)
with GuildLogs(guild) as logs:
# If "%cmd help" redirect to "%help cmd"
if "help" in args:
await client.bot.help(client, message, "help", args[0])
return
# check args validity
str_channel_mentions = [str(channel.id) for channel in message.channel_mentions]
str_channel_mentions = [
str(channel.id) for channel in message.channel_mentions
]
str_mentions = [str(member.id) for member in message.mentions]
for i, arg in enumerate(args[1:]):
if re.match(r"^<@!?\d+>$", arg):