Initial commit

This commit is contained in:
Pünkösd Marcell 2021-12-12 18:22:13 +01:00
commit 5e96b86641
2 changed files with 237 additions and 0 deletions

144
.gitignore vendored Normal file
View File

@ -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/

93
main.py Normal file
View File

@ -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()