From 5e96b866415b1215d4b248c069c0bdc3f0f29bdf Mon Sep 17 00:00:00 2001 From: marcsello Date: Sun, 12 Dec 2021 18:22:13 +0100 Subject: [PATCH] Initial commit --- .gitignore | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 93 ++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a659ef3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +#Pycharm +.idea/ +*.iml + +# Wing project file +*.wpr + +# Nano, vi, vim lockfile +*.swp + +# log +*.log + +ned_logs/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..1d057e1 --- /dev/null +++ b/main.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +import subprocess +import threading +from typing import Optional +from dataclasses import dataclass +import enum +import sys +import os +import io +import time + + +class DeviceState(enum.Enum): + CREATED = 0 + BOOTING = 1 + RUNNING = 2 + CRASHED = 3 + + +@dataclass +class DeviceInfo: + id: int + executable: str + state: DeviceState = DeviceState.CREATED + log_fd: Optional[io.TextIOWrapper] = None + proc: Optional[subprocess.Popen] = None + +class RealSlimShadyThread(threading.Thread): + + def __init__(self, device_list: list): + super().__init__() + self._device_list = device_list + self._device_list_lock = threading.Lock() + + def run(self): + while True: + time.sleep(0.5) + with self._device_list_lock: + + # Check for crashed devices + for device in filter(lambda dev: dev.state in [DeviceState.BOOTING, DeviceState.RUNNING], self._device_list): + if device.proc.pid: + ret = device.proc.poll() + + if (ret is None): # process is running + device.state = DeviceState.RUNNING + else: + device.state = DeviceState.CRASHED + + # Start created, crashed processes + for device in filter(lambda dev: dev.state == [DeviceState.CREATED, DeviceState.CRASHED], self._device_list)[:2]: + + if device.log_fd: + try: + device.log_fd.close() + except: + pass + + device.log_fd = open(f"ned_logs/{device.id}.log", "at") + device.proc = subprocess.Popen( + [device.executable, str(device.id)], + stdout=device.log_fd, + preexec_fn=lambda: os.setpgrp() + ) + + device.state = DeviceState.BOOTING + + def get_device_list(self) -> list: + with self._device_list_lock: + return self._device_list.copy() # swallow copy... eh + + +def main(): + executable = sys.argv[1] + n_proc = int(sys.argv[2]) + os.makedirs("ned_logs", exist_ok=True) + + device_list = [DeviceInfo(id=i, executable=executable) for i in range(n_proc)] + + while True: + print("\033[2J\033[0;0f", end="", flush=False) + for state in DeviceState: + print(state.name, flush=False) + print(", ".join(str(dev.id) for dev in device_list if dev.state == state), flush=False) + + print(flush=False) + + sys.stdout.flush() + time.sleep(1) + + +if __name__ == '__main__': + main()