winziprint.py: Extract daemon() function from main()
This commit is contained in:
@ -16,7 +16,7 @@ import weasyprint
|
|||||||
import pypdf
|
import pypdf
|
||||||
|
|
||||||
|
|
||||||
VERSION = __version__ = '0.2.3'
|
VERSION = __version__ = '0.2.4'
|
||||||
SOCKET_ADDRESS = ('127.0.0.1', 30983)
|
SOCKET_ADDRESS = ('127.0.0.1', 30983)
|
||||||
|
|
||||||
BATCH_SIZE = 10
|
BATCH_SIZE = 10
|
||||||
@ -166,6 +166,21 @@ def _wrapper_convert(args: list[str],
|
|||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
|
def daemon() -> None:
|
||||||
|
# a tcp server is used due to the lack of unix sockets on Windows
|
||||||
|
with socketserver.ThreadingTCPServer(SOCKET_ADDRESS, ConnectionHandler) as server:
|
||||||
|
def exit_gracefully(_signum: int, _frame) -> None:
|
||||||
|
raise KeyboardInterrupt()
|
||||||
|
signal.signal(signal.SIGINT, exit_gracefully)
|
||||||
|
signal.signal(signal.SIGTERM, exit_gracefully)
|
||||||
|
print('Running as daemon')
|
||||||
|
try:
|
||||||
|
server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('', file=sys.stderr)
|
||||||
|
print('Shutting down')
|
||||||
|
|
||||||
|
|
||||||
def usage(error: bool = False) -> None:
|
def usage(error: bool = False) -> None:
|
||||||
print(f'usage: {sys.argv[0]} [-h] [-v] [-d DIR] [ -D | [-p] [-2] [-e ENCODING] [ - | INPUT [INPUT...] OUTPUT ] ]\n'
|
print(f'usage: {sys.argv[0]} [-h] [-v] [-d DIR] [ -D | [-p] [-2] [-e ENCODING] [ - | INPUT [INPUT...] OUTPUT ] ]\n'
|
||||||
'\n'
|
'\n'
|
||||||
@ -233,20 +248,9 @@ def main() -> None:
|
|||||||
os.chdir(working_dir)
|
os.chdir(working_dir)
|
||||||
|
|
||||||
if '-D' in args:
|
if '-D' in args:
|
||||||
print('Running as daemon')
|
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
usage(True)
|
usage(True)
|
||||||
# a tcp server is used due to the lack of unix sockets on Windows
|
daemon()
|
||||||
with socketserver.ThreadingTCPServer(SOCKET_ADDRESS, ConnectionHandler) as server:
|
|
||||||
def exit_gracefully(_signum: int, _frame) -> None:
|
|
||||||
raise KeyboardInterrupt()
|
|
||||||
signal.signal(signal.SIGINT, exit_gracefully)
|
|
||||||
signal.signal(signal.SIGTERM, exit_gracefully)
|
|
||||||
try:
|
|
||||||
server.serve_forever()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print('', file=sys.stderr)
|
|
||||||
print('Shutting down')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
encoding = _get_arg(args, '-e', '--encoding')
|
encoding = _get_arg(args, '-e', '--encoding')
|
||||||
|
Reference in New Issue
Block a user