mirror of
https://github.com/k4yt3x/video2x.git
synced 2026-03-13 13:27:30 +08:00
fixed fail to exit upon ^C
This commit is contained in:
@@ -19,7 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
Name: Video Decoder
|
Name: Video Decoder
|
||||||
Author: K4YT3X
|
Author: K4YT3X
|
||||||
Date Created: June 17, 2021
|
Date Created: June 17, 2021
|
||||||
Last Modified: February 27, 2022
|
Last Modified: February 28, 2022
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
@@ -163,27 +163,26 @@ class VideoDecoder(threading.Thread):
|
|||||||
else:
|
else:
|
||||||
logger.debug("Decoding queue depleted")
|
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.flush()
|
||||||
self.decoder.stdout.close()
|
|
||||||
|
|
||||||
# flush the remaining data in STDERR and wait for it to be read
|
|
||||||
self.decoder.stderr.flush()
|
self.decoder.stderr.flush()
|
||||||
|
|
||||||
# send SIGINT (2) to FFmpeg
|
# send SIGINT (2) to FFmpeg
|
||||||
# this instructs it to finalize and exit
|
# this instructs it to finalize and exit
|
||||||
self.decoder.send_signal(signal.SIGINT)
|
self.decoder.send_signal(signal.SIGINT)
|
||||||
|
|
||||||
# wait for process to terminate
|
# close PIPEs to prevent process from getting stuck
|
||||||
self.pipe_printer.stop()
|
self.decoder.stdout.close()
|
||||||
self.decoder.stderr.close()
|
self.decoder.stderr.close()
|
||||||
|
|
||||||
# wait for processes and threads to stop
|
# wait for process to exit
|
||||||
self.pipe_printer.join()
|
|
||||||
self.decoder.wait()
|
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()
|
return super().run()
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
|
|||||||
@@ -167,27 +167,26 @@ class VideoEncoder(threading.Thread):
|
|||||||
else:
|
else:
|
||||||
logger.debug("Encoding queue depleted")
|
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.flush()
|
||||||
self.encoder.stdin.close()
|
|
||||||
|
|
||||||
# flush the remaining data in STDERR and wait for it to be read
|
|
||||||
self.encoder.stderr.flush()
|
self.encoder.stderr.flush()
|
||||||
|
|
||||||
# send SIGINT (2) to FFmpeg
|
# send SIGINT (2) to FFmpeg
|
||||||
# this instructs it to finalize and exit
|
# this instructs it to finalize and exit
|
||||||
self.encoder.send_signal(signal.SIGINT)
|
self.encoder.send_signal(signal.SIGINT)
|
||||||
|
|
||||||
# wait for process to terminate
|
# close PIPEs to prevent process from getting stuck
|
||||||
self.pipe_printer.stop()
|
self.encoder.stdin.close()
|
||||||
self.encoder.stderr.close()
|
self.encoder.stderr.close()
|
||||||
|
|
||||||
# wait for processes and threads to stop
|
# wait for process to exit
|
||||||
self.pipe_printer.join()
|
|
||||||
self.encoder.wait()
|
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()
|
return super().run()
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
Name: Interpolator
|
Name: Interpolator
|
||||||
Author: K4YT3X
|
Author: K4YT3X
|
||||||
Date Created: May 27, 2021
|
Date Created: May 27, 2021
|
||||||
Last Modified: February 16, 2022
|
Last Modified: February 28, 2022
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
@@ -113,7 +113,6 @@ class Interpolator(multiprocessing.Process):
|
|||||||
break
|
break
|
||||||
|
|
||||||
logger.info(f"Interpolator process {self.name} terminating")
|
logger.info(f"Interpolator process {self.name} terminating")
|
||||||
self.running = False
|
|
||||||
return super().run()
|
return super().run()
|
||||||
|
|
||||||
def _stop(self, _signal_number, _frame) -> None:
|
def _stop(self, _signal_number, _frame) -> None:
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
Name: PIPE Printer
|
Name: PIPE Printer
|
||||||
Author: K4YT3X
|
Author: K4YT3X
|
||||||
Date Created: February 27, 2022
|
Date Created: February 27, 2022
|
||||||
Last Modified: February 27, 2022
|
Last Modified: February 28, 2022
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# built-in imports
|
# built-in imports
|
||||||
@@ -39,6 +39,11 @@ class PipePrinter(threading.Thread):
|
|||||||
# set read mode to non-blocking
|
# set read mode to non-blocking
|
||||||
os.set_blocking(self.stderr.fileno(), False)
|
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:
|
def run(self) -> None:
|
||||||
self.running = True
|
self.running = True
|
||||||
|
|
||||||
@@ -46,9 +51,12 @@ class PipePrinter(threading.Thread):
|
|||||||
while self.running:
|
while self.running:
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
output = self.stderr.read()
|
try:
|
||||||
if output is not None:
|
self._print_output()
|
||||||
print(output.decode(), file=sys.stderr)
|
|
||||||
|
# pipe closed
|
||||||
|
except ValueError:
|
||||||
|
break
|
||||||
|
|
||||||
return super().run()
|
return super().run()
|
||||||
|
|
||||||
|
|||||||
@@ -190,7 +190,6 @@ class Upscaler(multiprocessing.Process):
|
|||||||
logger.opt(colors=True).info(
|
logger.opt(colors=True).info(
|
||||||
f"Upscaler process <blue>{self.name}</blue> terminating"
|
f"Upscaler process <blue>{self.name}</blue> terminating"
|
||||||
)
|
)
|
||||||
self.running = False
|
|
||||||
return super().run()
|
return super().run()
|
||||||
|
|
||||||
def _stop(self, _signal_number, _frame) -> None:
|
def _stop(self, _signal_number, _frame) -> None:
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ __ __ _ _ ___ __ __
|
|||||||
Name: Video2X
|
Name: Video2X
|
||||||
Creator: K4YT3X
|
Creator: K4YT3X
|
||||||
Date Created: February 24, 2018
|
Date Created: February 24, 2018
|
||||||
Last Modified: February 27, 2022
|
Last Modified: February 28, 2022
|
||||||
|
|
||||||
Editor: BrianPetkovsek
|
Editor: BrianPetkovsek
|
||||||
Last Modified: June 17, 2019
|
Last Modified: June 17, 2019
|
||||||
@@ -290,14 +290,7 @@ class Video2X:
|
|||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
exception.append(e)
|
exception.append(e)
|
||||||
|
|
||||||
# if no exceptions were produced
|
|
||||||
else:
|
|
||||||
logger.success("Processing completed successfully")
|
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# mark processing queue as closed
|
|
||||||
self.processing_queue.close()
|
|
||||||
|
|
||||||
# stop processor processes
|
# stop processor processes
|
||||||
logger.info("Stopping processor processes")
|
logger.info("Stopping processor processes")
|
||||||
for process in self.processor_processes:
|
for process in self.processor_processes:
|
||||||
@@ -307,13 +300,16 @@ class Video2X:
|
|||||||
for process in self.processor_processes:
|
for process in self.processor_processes:
|
||||||
process.join()
|
process.join()
|
||||||
|
|
||||||
# ensure both the decoder and the encoder have exited
|
# stop encoder and decoder
|
||||||
logger.info("Stopping decoder and encoder threads")
|
logger.info("Stopping decoder and encoder threads")
|
||||||
self.decoder.stop()
|
self.decoder.stop()
|
||||||
self.encoder.stop()
|
self.encoder.stop()
|
||||||
self.decoder.join()
|
self.decoder.join()
|
||||||
self.encoder.join()
|
self.encoder.join()
|
||||||
|
|
||||||
|
# mark processing queue as closed
|
||||||
|
self.processing_queue.close()
|
||||||
|
|
||||||
# raise the error if there is any
|
# raise the error if there is any
|
||||||
if len(exception) > 0:
|
if len(exception) > 0:
|
||||||
raise exception[0]
|
raise exception[0]
|
||||||
@@ -549,7 +545,7 @@ def main() -> int:
|
|||||||
"<magenta>Copyright (C) 2018-2022 K4YT3X and contributors.</magenta>"
|
"<magenta>Copyright (C) 2018-2022 K4YT3X and contributors.</magenta>"
|
||||||
)
|
)
|
||||||
|
|
||||||
# initialize upscaler object
|
# initialize video2x object
|
||||||
video2x = Video2X()
|
video2x = Video2X()
|
||||||
|
|
||||||
if args.action == "upscale":
|
if args.action == "upscale":
|
||||||
@@ -573,8 +569,6 @@ def main() -> int:
|
|||||||
args.algorithm,
|
args.algorithm,
|
||||||
)
|
)
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# don't print the traceback for manual terminations
|
# don't print the traceback for manual terminations
|
||||||
except KeyboardInterrupt as e:
|
except KeyboardInterrupt as e:
|
||||||
return 2
|
return 2
|
||||||
@@ -582,3 +576,8 @@ def main() -> int:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# if no exceptions were produced
|
||||||
|
else:
|
||||||
|
logger.success("Processing completed successfully")
|
||||||
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user