Skip to content

Commit

Permalink
Fix memory leak from scroll view logs (#85)
Browse files Browse the repository at this point in the history
Replace it with a normal `Widget` with a `Panel` that displays the most recent logs
  • Loading branch information
d-j-hatton authored Sep 8, 2022
1 parent 8a7c461 commit e690924
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/murfey/client/customlogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import List

from rich.console import Console, RenderableType
from rich.console import RenderableType
from rich.containers import Renderables
from rich.logging import RichHandler
from rich.text import Text
Expand Down Expand Up @@ -37,7 +37,6 @@ def __init__(self, queue: Queue, **kwargs):
super().__init__(**kwargs)
self._queue = queue
self.redirect = False
self._console = Console()
self._last_time = None

def get_log_row(self, record, message_renderable) -> list:
Expand All @@ -46,7 +45,7 @@ def get_log_row(self, record, message_renderable) -> list:
path = Path(record.pathname).name
level = self.get_level_text(record)
time_format = None if self.formatter is None else self.formatter.datefmt
log_time = datetime.fromtimestamp(record.created) or self.console.get_datetime()
log_time = datetime.fromtimestamp(record.created) or datetime.now()
time_format = time_format or self._log_render.time_format
link_path = record.pathname if self.enable_link_path else None
if callable(time_format):
Expand Down
23 changes: 16 additions & 7 deletions src/murfey/client/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# import asyncio
# import contextlib
import copy
import logging
import string
import threading
Expand All @@ -13,7 +14,7 @@

import requests
from pydantic import BaseModel, ValidationError
from rich.box import SQUARE
from rich.box import MINIMAL, SQUARE
from rich.logging import RichHandler
from rich.panel import Panel
from textual import events
Expand Down Expand Up @@ -377,7 +378,7 @@ async def on_key(self, key: events.Key) -> None:
key.stop()


class LogBook(ScrollView):
class LogBook(Widget):
def __init__(self, queue, *args, **kwargs):
self._queue = queue
self._next_log = None
Expand All @@ -400,6 +401,13 @@ def _load_from_queue(self) -> bool:
return True
return False

def render(self) -> Panel:
panel_msg = self._logs or ""
return Panel(
panel_msg,
box=MINIMAL,
)

async def tick(self):
loaded = self._load_from_queue()
if loaded:
Expand All @@ -412,13 +420,14 @@ async def tick(self):
for nl in self._next_log:
self._log_cache.append(nl)
self._logs.add_row(*nl[0])
await self.update(self._logs, home=False)
if len(self._logs.rows) > 50:
if len(self._log_cache) > 50:
self._logs = self._log_cache[-50][1]
for r in self._log_cache[-49:]:
curr_log_cache = copy.deepcopy(self._log_cache)
for r in curr_log_cache[-49:]:
self._logs.add_row(*r[0])
self._log_cache = self._log_cache[-50:]
self.page_down()
self._log_cache = curr_log_cache[-50:]
del curr_log_cache
self.refresh()


class DCParametersTomo(BaseModel):
Expand Down
3 changes: 0 additions & 3 deletions src/murfey/client/watchdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def __repr__(self) -> str:

def scan(self, modification_time: float | None = None, transfer_all: bool = False):
try:
# t_start = time.perf_counter()
filelist = self._scan_directory(
modification_time=self._modification_overwrite or modification_time
)
# t_scan = time.perf_counter() - t_start
# log.info(f"Scan of {self._basepath} completed in {t_scan:.1f} seconds")
scan_completion = time.time()

for entry, entry_info in filelist.items():
Expand Down

0 comments on commit e690924

Please sign in to comment.