Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 12, 2024
1 parent 94360fb commit fca3fca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
25 changes: 12 additions & 13 deletions src/tqdm_publisher/_demo/_demo_command_line_interface.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import signal
import subprocess
import sys
import time
from pathlib import Path

from ._server import run_demo as _server
from ._parallel._server import run_demo as _parallel_server

import signal
import time
from ._server import run_demo as _server

DEMO_BASE_FOLDER_PATH = Path(__file__).parent

Expand All @@ -24,11 +23,13 @@

SUBPROCESSES = []


def close_process():
for process in SUBPROCESSES:
process.terminate() # Send SIGTERM to subprocess
# sys.exit()


def signal_handler(signal, frame):
close_process()

Expand All @@ -51,7 +52,7 @@ def run_demo(demo, flags):

_server()

elif demo == 'parallel-demo':
elif demo == "parallel-demo":
if flags["client"]:
client_args = ["python", PARALLEL_CLIENT_FILE_PATH]
if flags["both"]:
Expand All @@ -63,12 +64,11 @@ def run_demo(demo, flags):
if flags["server"]:
if flags["both"]:
_parallel_server(flags["host"], flags["port"])
else:
else:
_parallel_server()

close_process()


else:
print(f"{demo} is an invalid demo option.")

Expand All @@ -79,9 +79,8 @@ def get_flag(flags, flag, default=None):
return flags[flag_index + 1]
return default

def _command_line_interface():


def _command_line_interface():
"""A simple command line interface for running the demo for TQDM Publisher."""
if len(sys.argv) <= 1:
print("No input provided. Please specify a command (e.g. 'demo').")
Expand All @@ -99,7 +98,7 @@ def _command_line_interface():
if len(flags_list) > 0:
print(f"No flags are accepted at this time, but flags {flags_list} were received.")
return

client_flag = "--client" in flags_list
server_flag = "--server" in flags_list
both_flags = "--server" in flags_list and "--client" in flags_list
Expand All @@ -108,13 +107,13 @@ def _command_line_interface():
client=not server_flag or both_flags,
server=not client_flag or both_flags,
both=(client_flag and server_flag) or (not client_flag and not server_flag),
host= get_flag(flags_list, HOST_FLAG, "localhost"),
port= get_flag(flags_list, PORT_FLAG, 8000)
host=get_flag(flags_list, HOST_FLAG, "localhost"),
port=get_flag(flags_list, PORT_FLAG, 8000),
)

try:
run_demo(command, flags)
except KeyboardInterrupt as e:
print("\n\nInterrupt signal received. Shutting down subprocesses...")
finally:
close_process()
close_process()
8 changes: 4 additions & 4 deletions src/tqdm_publisher/_demo/_parallel/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
# HTTP server addition
import http.server
import json

# Kill server on interrupt
import signal
import socket
import socketserver
import sys

# Kill server on interrupt
import signal


def find_free_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0)) # Bind to a free port provided by the host
return s.getsockname()[1] # Return the port number assigned



class MyTCPServer(socketserver.TCPServer):
allow_reuse_address = True # This allows the server to bind to an address that is in a TIME_WAIT state

Expand Down
3 changes: 2 additions & 1 deletion src/tqdm_publisher/_demo/_parallel/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uuid
from concurrent.futures import ProcessPoolExecutor
from typing import Tuple

import requests

from tqdm_publisher import TQDMPublisher
Expand Down Expand Up @@ -76,4 +77,4 @@ def run_demo(host, port):
host_index = flags_list.index("--host")
HOST = flags_list[host_index + 1]

run_demo(HOST, PORT)
run_demo(HOST, PORT)

0 comments on commit fca3fca

Please sign in to comment.