Skip to content

Commit

Permalink
馃悰 Get rid of busy subprocess loop
Browse files Browse the repository at this point in the history
Closes #4993
  • Loading branch information
foosel committed Apr 25, 2024
1 parent 84f882b commit 8018abd
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/octoprint/filemanager/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ def __init__(self, finished_callback):

self._aborted = False
self._reenqueue = False
self._command = None

def _do_analysis(self, high_priority=False):
import sys
Expand Down Expand Up @@ -482,7 +483,7 @@ def _do_analysis(self, high_priority=False):
command += ["--g90-extruder"]
command.append(self._current.absolute_path)

self._logger.info("Invoking analysis command: {}".format(" ".join(command)))
self._logger.info(f"Invoking analysis command: {' '.join(command)}")

self._aborted = False
p = sarge.run(
Expand All @@ -496,28 +497,27 @@ def _do_analysis(self, high_priority=False):
# thread
time.sleep(0.01)

# by now we should have a command, let's wait for its
# process to have been prepared
p.commands[0].process_ready.wait()

if not p.commands[0].process:
# the process might have been set to None in case of any exception
raise RuntimeError(
"Error while trying to run command {}".format(" ".join(command))
)

try:
# let's wait for stuff to finish
while p.returncode is None:
# by now we should have a command, let's wait for its
# process to have been prepared
self._command = p.commands[0]
self._command.process_ready.wait()

if not self._command.process:
# the process might have been set to None in case of any exception
raise RuntimeError(
"Error while trying to run command {}".format(" ".join(command))
)

try:
# let's wait for stuff to finish
self._command.wait()
if self._aborted:
# oh, we shall abort, let's do so!
p.commands[0].terminate()
raise AnalysisAborted(reenqueue=self._reenqueue)

# else continue
p.commands[0].poll()
finally:
p.close()
finally:
p.close()
self._command = None

output = p.stdout.text
self._logger.debug(f"Got output: {output!r}")
Expand Down Expand Up @@ -559,3 +559,7 @@ def _do_analysis(self, high_priority=False):
def _do_abort(self, reenqueue=True):
self._aborted = True
self._reenqueue = reenqueue

if self._command:
self._logger.info(f"Terminating analysis subprocess for {self._current}...")
self._command.terminate()

0 comments on commit 8018abd

Please sign in to comment.