Multithread

mail@pastecode.io avatar
unknown
python
2 months ago
1.4 kB
8
Indexable
Never
import sys
from concurrent.futures import ThreadPoolExecutor
import subprocess
import logging


def run_script(script_path):
    # Configure logger for the script
    # Get logger
    logger = logging.getLogger(script_path)
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
    # Create a handler
    f_handler = logging.FileHandler(f"logs/{script_path}.log", mode="a")
    f_handler.setLevel(logging.DEBUG)
    s_handler = logging.StreamHandler(sys.stdout)
    s_handler.setLevel(logging.DEBUG)
    logger.addHandler(f_handler)
    # logger.addHandler(s_handler)
    # Set logging level to the logger
    logger.setLevel(logging.DEBUG)

    process = subprocess.Popen(["python", script_path], stdout=subprocess.PIPE, shell=True)
    return logger, process


def read_output(logger, process):
    while True:
        line = process.stdout.readline().decode()
        if not line:
            break
        logger.info(line.rstrip())


if __name__ == "__main__":
    script_paths = ["script1.py", "script2.py", "script3.py"]

    with ThreadPoolExecutor() as executor:
        futures = [executor.submit(run_script, script_path) for script_path in script_paths]

        for future in futures:
            logger, process = future.result()
            read_output(logger, process)

Leave a Comment