Skip to content

Commit

Permalink
fixed fail to exit upon ^C
Browse files Browse the repository at this point in the history
  • Loading branch information
k4yt3x committed Feb 28, 2022
1 parent 49e0375 commit 2bfcb13
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
21 changes: 10 additions & 11 deletions video2x/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Name: Video Decoder
Author: K4YT3X
Date Created: June 17, 2021
Last Modified: February 27, 2022
Last Modified: February 28, 2022
"""

# local imports
Expand Down Expand Up @@ -163,27 +163,26 @@ def run(self) -> None:
else:
logger.debug("Decoding queue depleted")

# flush the remaining data in STDOUT and close PIPE
# flush the remaining data in STDOUT and STDERR
self.decoder.stdout.flush()
self.decoder.stdout.close()

# flush the remaining data in STDERR and wait for it to be read
self.decoder.stderr.flush()

# send SIGINT (2) to FFmpeg
# this instructs it to finalize and exit
self.decoder.send_signal(signal.SIGINT)

# wait for process to terminate
self.pipe_printer.stop()
# close PIPEs to prevent process from getting stuck
self.decoder.stdout.close()
self.decoder.stderr.close()

# wait for processes and threads to stop
self.pipe_printer.join()
# wait for process to exit
self.decoder.wait()
logger.info("Decoder thread exiting")

self.running = False
# wait for PIPE printer to exit
self.pipe_printer.stop()
self.pipe_printer.join()

logger.info("Decoder thread exiting")
return super().run()

def stop(self) -> None:
Expand Down
19 changes: 9 additions & 10 deletions video2x/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,26 @@ def run(self) -> None:
else:
logger.debug("Encoding queue depleted")

# flush the remaining data in STDIN and close PIPE
# flush the remaining data in STDIN and STDERR
self.encoder.stdin.flush()
self.encoder.stdin.close()

# flush the remaining data in STDERR and wait for it to be read
self.encoder.stderr.flush()

# send SIGINT (2) to FFmpeg
# this instructs it to finalize and exit
self.encoder.send_signal(signal.SIGINT)

# wait for process to terminate
self.pipe_printer.stop()
# close PIPEs to prevent process from getting stuck
self.encoder.stdin.close()
self.encoder.stderr.close()

# wait for processes and threads to stop
self.pipe_printer.join()
# wait for process to exit
self.encoder.wait()
logger.info("Encoder thread exiting")

self.running = False
# wait for PIPE printer to exit
self.pipe_printer.stop()
self.pipe_printer.join()

logger.info("Encoder thread exiting")
return super().run()

def stop(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions video2x/interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Name: Interpolator
Author: K4YT3X
Date Created: May 27, 2021
Last Modified: February 16, 2022
Last Modified: February 28, 2022
"""

# local imports
Expand Down Expand Up @@ -113,7 +113,6 @@ def run(self) -> None:
break

logger.info(f"Interpolator process {self.name} terminating")
self.running = False
return super().run()

def _stop(self, _signal_number, _frame) -> None:
Expand Down
16 changes: 12 additions & 4 deletions video2x/pipe_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Name: PIPE Printer
Author: K4YT3X
Date Created: February 27, 2022
Last Modified: February 27, 2022
Last Modified: February 28, 2022
"""

# built-in imports
Expand All @@ -39,16 +39,24 @@ def __init__(self, stderr: IO[bytes]) -> None:
# set read mode to non-blocking
os.set_blocking(self.stderr.fileno(), False)

def _print_output(self) -> None:
output = self.stderr.read()
if output is not None and len(output) != 0:
print(output.decode(), file=sys.stderr)

def run(self) -> None:
self.running = True

# keep printing contents in the PIPE
while self.running:
time.sleep(0.5)

output = self.stderr.read()
if output is not None:
print(output.decode(), file=sys.stderr)
try:
self._print_output()

# pipe closed
except ValueError:
break

return super().run()

Expand Down
1 change: 0 additions & 1 deletion video2x/upscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def run(self) -> None:
logger.opt(colors=True).info(
f"Upscaler process <blue>{self.name}</blue> terminating"
)
self.running = False
return super().run()

def _stop(self, _signal_number, _frame) -> None:
Expand Down
23 changes: 11 additions & 12 deletions video2x/video2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Name: Video2X
Creator: K4YT3X
Date Created: February 24, 2018
Last Modified: February 27, 2022
Last Modified: February 28, 2022
Editor: BrianPetkovsek
Last Modified: June 17, 2019
Expand Down Expand Up @@ -290,14 +290,7 @@ def _run(
logger.exception(e)
exception.append(e)

# if no exceptions were produced
else:
logger.success("Processing completed successfully")

finally:
# mark processing queue as closed
self.processing_queue.close()

# stop processor processes
logger.info("Stopping processor processes")
for process in self.processor_processes:
Expand All @@ -307,13 +300,16 @@ def _run(
for process in self.processor_processes:
process.join()

# ensure both the decoder and the encoder have exited
# stop encoder and decoder
logger.info("Stopping decoder and encoder threads")
self.decoder.stop()
self.encoder.stop()
self.decoder.join()
self.encoder.join()

# mark processing queue as closed
self.processing_queue.close()

# raise the error if there is any
if len(exception) > 0:
raise exception[0]
Expand Down Expand Up @@ -549,7 +545,7 @@ def main() -> int:
"<magenta>Copyright (C) 2018-2022 K4YT3X and contributors.</magenta>"
)

# initialize upscaler object
# initialize video2x object
video2x = Video2X()

if args.action == "upscale":
Expand All @@ -573,12 +569,15 @@ def main() -> int:
args.algorithm,
)

return 0

# don't print the traceback for manual terminations
except KeyboardInterrupt as e:
return 2

except Exception as e:
logger.exception(e)
return 1

# if no exceptions were produced
else:
logger.success("Processing completed successfully")
return 0

0 comments on commit 2bfcb13

Please sign in to comment.