First test runs with multiprocessing.
This commit is contained in:
		
							
								
								
									
										82
									
								
								ircthing.py
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								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_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 = {}
 | 
					Processes = {}
 | 
				
			||||||
Stop_Toggle = threading.Event()
 | 
					Stop_Toggle = multiprocessing.Event()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def clean_exit(fifo_files, socket):
 | 
					def clean_exit():
 | 
				
			||||||
    socket.send(bytes(f"QUIT :Bye\r\n", "UTF-8"))
 | 
					 | 
				
			||||||
    Stop_Toggle.set()
 | 
					    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():
 | 
					def main():
 | 
				
			||||||
    my_name = sys.argv[0]
 | 
					    root_path = base_path()
 | 
				
			||||||
    my_name_pyless, _ = os.path.splitext(my_name)
 | 
					 | 
				
			||||||
    base_path = f'/tmp/{my_name_pyless}'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    config_path = '../ircthing3.ini'
 | 
					    # Get configuration file path if given
 | 
				
			||||||
 | 
					    config_path = 'config.ini'
 | 
				
			||||||
    # Read configurations for all topics
 | 
					    argument = cli_args()
 | 
				
			||||||
 | 
					    if argument:
 | 
				
			||||||
 | 
					        config_path = argument
 | 
				
			||||||
 | 
					    # Read configuration 
 | 
				
			||||||
    network_configs = read_config(config_path)
 | 
					    network_configs = read_config(config_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ## Get irc socket for each network in configuration
 | 
					    ## Get irc socket for each network in configuration
 | 
				
			||||||
    ## Start thread for each socket 
 | 
					    ## Start thread for each socket 
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    for network in network_configs:
 | 
					    for network in network_configs:
 | 
				
			||||||
        net_name = network["net_name"]
 | 
					        net_name = network["net_name"]
 | 
				
			||||||
        print(f"{time.time()} | Found configs for {net_name} network.")
 | 
					 | 
				
			||||||
        server = network["server"]
 | 
					        server = network["server"]
 | 
				
			||||||
        port = network["port"]
 | 
					        port = network["port"]
 | 
				
			||||||
        nickname = network["nickname"]
 | 
					        nickname = network["nickname"]
 | 
				
			||||||
        password = network["password"]
 | 
					        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)
 | 
					        print(f"{time.time()} | Found configs for {net_name} network.")
 | 
				
			||||||
        Threads[net_name] = threading.Thread(target=router_instance.start)
 | 
					        irc_socket, fifo_files, network_dir = connect_to_irc_server(root_path, net_name, server, port, nickname, password)
 | 
				
			||||||
        Threads[net_name].daemon = True
 | 
					 | 
				
			||||||
        Threads[net_name].start()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for thread in Threads.values():
 | 
					        router_instance = irc_router(fifo_files, irc_socket, server, nickname, network_dir)
 | 
				
			||||||
        print(thread)
 | 
					        Processes[net_name] = multiprocessing.Process(target=router_instance.start)
 | 
				
			||||||
        thread.join()
 | 
					        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__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
@@ -58,5 +75,6 @@ if __name__ == "__main__":
 | 
				
			|||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        print(f"Got error {e}")
 | 
					        print(f"Got error {e}")
 | 
				
			||||||
    finally:
 | 
					    finally:
 | 
				
			||||||
 | 
					        clean_exit()
 | 
				
			||||||
        print(f"{time.time()} | Bye!")
 | 
					        print(f"{time.time()} | Bye!")
 | 
				
			||||||
        exit(0)
 | 
					        exit(0)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,17 +29,18 @@ def connect_to_irc_server(
 | 
				
			|||||||
    irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8"))
 | 
					    irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Create directories for the server and channel
 | 
					    # 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:
 | 
					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.fifo_files = fifo_files
 | 
				
			||||||
        self.irc_socket = irc_socket
 | 
					        self.irc_socket = irc_socket
 | 
				
			||||||
        self.server = server
 | 
					        self.server = server
 | 
				
			||||||
        self.nickname = nickname
 | 
					        self.nickname = nickname
 | 
				
			||||||
 | 
					        self.network_dir = network_dir
 | 
				
			||||||
        self.channels = []
 | 
					        self.channels = []
 | 
				
			||||||
        self._loop = asyncio.new_event_loop()
 | 
					        self._loop = asyncio.new_event_loop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,21 @@
 | 
				
			|||||||
import configparser
 | 
					import configparser
 | 
				
			||||||
 | 
					import argparse
 | 
				
			||||||
import os
 | 
					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):
 | 
					def read_config(config_path):
 | 
				
			||||||
    config = configparser.ConfigParser()
 | 
					    config = configparser.ConfigParser()
 | 
				
			||||||
@@ -47,5 +63,5 @@ def make_files(path, net_name):
 | 
				
			|||||||
    fifo_files = []
 | 
					    fifo_files = []
 | 
				
			||||||
    fifo_files.append(f"{server_dir}/in")
 | 
					    fifo_files.append(f"{server_dir}/in")
 | 
				
			||||||
    fifo_files.append(f"{server_dir}/out")
 | 
					    fifo_files.append(f"{server_dir}/out")
 | 
				
			||||||
    return fifo_files
 | 
					    return fifo_files, server_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user