From 1f542b17207ae9c0de66ad7d7a433c205061f590 Mon Sep 17 00:00:00 2001 From: kalzu rekku Date: Fri, 9 Feb 2024 09:14:24 +0200 Subject: [PATCH] First test runs with multiprocessing. --- ircthing.py | 82 +++++++++++++++++++++++++++++------------------ ircthing_core.py | 7 ++-- ircthing_utils.py | 18 ++++++++++- 3 files changed, 71 insertions(+), 36 deletions(-) diff --git a/ircthing.py b/ircthing.py index 6492fdc..82e36bf 100644 --- a/ircthing.py +++ b/ircthing.py @@ -1,54 +1,71 @@ -import threading -import os -import sys -import time -import socket from ircthing_core import irc_router, connect_to_irc_server -from ircthing_utils import read_config +from ircthing_utils import read_config, cli_args, base_path +import time +import os +import multiprocessing -Threads = {} -Stop_Toggle = threading.Event() +Processes = {} +Stop_Toggle = multiprocessing.Event() -def clean_exit(fifo_files, socket): - socket.send(bytes(f"QUIT :Bye\r\n", "UTF-8")) +def clean_exit(): Stop_Toggle.set() - for file in fifo_files: - try: - os.unlink(file) - except FileNotFoundError: - # We are okay if some fifo files has been removed before this. - pass def main(): - my_name = sys.argv[0] - my_name_pyless, _ = os.path.splitext(my_name) - base_path = f'/tmp/{my_name_pyless}' + root_path = base_path() - config_path = '../ircthing3.ini' - - # Read configurations for all topics + # Get configuration file path if given + config_path = 'config.ini' + argument = cli_args() + if argument: + config_path = argument + # Read configuration network_configs = read_config(config_path) ## Get irc socket for each network in configuration ## Start thread for each socket - for network in network_configs: net_name = network["net_name"] - print(f"{time.time()} | Found configs for {net_name} network.") server = network["server"] port = network["port"] nickname = network["nickname"] password = network["password"] - irc_socket, fifo_files = connect_to_irc_server(base_path, net_name, server, port, nickname, password) - router_instance = irc_router(fifo_files, irc_socket, server, nickname) - Threads[net_name] = threading.Thread(target=router_instance.start) - Threads[net_name].daemon = True - Threads[net_name].start() + print(f"{time.time()} | Found configs for {net_name} network.") + irc_socket, fifo_files, network_dir = connect_to_irc_server(root_path, net_name, server, port, nickname, password) - for thread in Threads.values(): - print(thread) - thread.join() + router_instance = irc_router(fifo_files, irc_socket, server, nickname, network_dir) + Processes[net_name] = multiprocessing.Process(target=router_instance.start) + Processes[net_name].daemon = True + Processes[net_name].start() + + main_handle(root_path) + + for process in Processes.values(): + process.join() + +def main_handle(path): + input = f"{path}/in" + output = f"{path}/out" + os.mkfifo(input) + os.mkfifo(output) + while True: + line = read_input(input) + write_output(output, line) + +def read_input(file): + with open(file, 'r') as input: + line = input.readline().strip() + if not line: + return + if line == "exit": + clean_exit() + return line + +def write_output(file, line): + with open(file, 'a') as output: + output.write(f"{time.time()} | {line}\r\n") + output.flush() + output.close() if __name__ == "__main__": @@ -58,5 +75,6 @@ if __name__ == "__main__": except Exception as e: print(f"Got error {e}") finally: + clean_exit() print(f"{time.time()} | Bye!") exit(0) diff --git a/ircthing_core.py b/ircthing_core.py index 7e3492c..15f885c 100644 --- a/ircthing_core.py +++ b/ircthing_core.py @@ -29,17 +29,18 @@ def connect_to_irc_server( irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8")) # Create directories for the server and channel - fifo_files = make_files(base_path, net_name) + fifo_files, network_path = make_files(base_path, net_name) - return irc_socket, fifo_files + return irc_socket, fifo_files, network_path class irc_router: - def __init__(self, fifo_files, irc_socket, server, nickname): + def __init__(self, fifo_files, irc_socket, server, nickname, network_dir): self.fifo_files = fifo_files self.irc_socket = irc_socket self.server = server self.nickname = nickname + self.network_dir = network_dir self.channels = [] self._loop = asyncio.new_event_loop() diff --git a/ircthing_utils.py b/ircthing_utils.py index fcde6ef..5c52433 100644 --- a/ircthing_utils.py +++ b/ircthing_utils.py @@ -1,5 +1,21 @@ import configparser +import argparse import os +import sys + +def base_path(): + my_name = sys.argv[0] + my_name_pyless, _ = os.path.splitext(my_name) + return f'/tmp/{my_name_pyless}' + +def cli_args(): + parser = argparse.ArgumentParser(description="Usage: python3.11 ircthing.py /myconfig.ini") + parser.add_argument('config', help="Path to the configuration file.") + + args = parser.parse_args() + if args.config: + return args.config + return None def read_config(config_path): config = configparser.ConfigParser() @@ -47,5 +63,5 @@ def make_files(path, net_name): fifo_files = [] fifo_files.append(f"{server_dir}/in") fifo_files.append(f"{server_dir}/out") - return fifo_files + return fifo_files, server_dir