036e3ee2ce42 draft — Alain Leufroy 11 months ago
🚸 reduce cpu usage

- kill subprocess in a better way
- call patch subprocess more sequentially
2 files changed, 22 insertions(+), 8 deletions(-)

M lairucrem/controler.py
M lairucrem/process.py
M lairucrem/controler.py +20 -7
@@ 43,6 43,9 @@ class _multiwalker(
         await asyncio.gather(*(walker.wait() for walker in self.__sequences__))
         urwid.emit_signal(self, 'completed')
 
+    async def stop(self):
+        await asyncio.gather(*(walker.stop() for walker in self.__sequences__))
+
 
 class _hgwalker(ensurable, waitable, urwid.SimpleFocusListWalker):
     """An waitable walker though stdout of an async hg process.

          
@@ 61,6 64,7 @@ class _hgwalker(ensurable, waitable, urw
         pass
 
     def __init__(self, *args, **kwargs):
+        self.hgproc = None
         super().__init__([urwid.Text('LOADING …')], *args, **kwargs)
 
     def get_cmd(self):

          
@@ 76,6 80,8 @@ class _hgwalker(ensurable, waitable, urw
         cmd = self.get_cmd()
         if not cmd:
             return
+        if self.hgproc:
+            await self.hgproc.kill()
         self.hgproc = process.hg(*cmd)
         await self.hgproc.start()
         try:

          
@@ 110,11 116,15 @@ class _hgwalker(ensurable, waitable, urw
         urwid.emit_signal(self, 'completed')
 
     def clear(self, full=False):
-        self.cancel()
         super().clear()
         if not full:
             self.append(urwid.Text('LOADING …'))
 
+    async def stop(self):
+        if self.hgproc:
+            await self.hgproc.kill()
+        super().cancel()
+
 
 class _listbox(ensurable, waitable, mixin.mouse_up_down, urwid.ListBox):
     """An waitable listbox that handle waitable walkers."""

          
@@ 153,6 163,9 @@ class _listbox(ensurable, waitable, mixi
     def cancel(self):
         self.body.cancel()
 
+    async def stop(self):
+        await self.body.stop()
+
     def set_focus(self, position, coming_from=None):
         try:
             return super().set_focus(position, coming_from)

          
@@ 597,8 610,8 @@ class patchlistwalker(_multiwalker):
             lambda *args: urwid.emit_signal(self, 'jump_diff', *args)
         )
 
-    def reset_node(self, node):
-        self.cancel()
+    async def reset_node(self, node):
+        await self.stop()
         self.node = node
         self._summary.node = node
         self._summaryextra.node = node

          
@@ 643,13 656,13 @@ class patchlistbox(
         except urwid.ListBoxError:
             pass
 
-    def reset_node(self, node):
+    async def reset_node(self, node):
         # resetonly if not CWD and changed. We always reset on cwd as
         # the user evently want to view changes while cset is expected
         # not to be modified outside the application.
         if node is not None and node == self.body.node:
             return
-        self.body.reset_node(node)
+        await self.body.reset_node(node)
         self.ensure()
 
     def _iter_widgets(self, startpos=None, forward=True):

          
@@ 723,8 736,8 @@ class maincontroler(ensurable):
             mainwidget.pane(self._patch, 'PATCH'),
         ]))
 
-    def _on_focus_patch(self, *args):
-        self._patch.reset_node(self._graph.current_node)
+    async def _on_focus_patch(self, *args):
+        await self._patch.reset_node(self._graph.current_node)
 
     def ensure(self):
         self._graph.ensure()

          
M lairucrem/process.py +2 -1
@@ 103,12 103,13 @@ class hg:
                 continue
 
     async def stop(self):
+        if not self.proc:
+            return
         try:
             await _wait_process(self.proc, cmd=self.cmd)
         except OSError as exc:
             if exc.errno == 255:
                 raise RepositoryNotFound(str(exc))
-
         self.proc = None
 
     async def kill(self):