import subprocess import sys import time from pathlib import Path BASE_DIR = Path(__file__).resolve().parent telegram_file = BASE_DIR / "telebot.py" s3_worker_file = BASE_DIR / "rub.py" telegram_proc = None s3_worker_proc = None try: s3_worker_proc = subprocess.Popen([sys.executable, str(s3_worker_file)]) telegram_proc = subprocess.Popen([sys.executable, str(telegram_file)]) print( f"Started S3 worker pid={s3_worker_proc.pid} and Telegram worker pid={telegram_proc.pid}", flush=True, ) while True: if s3_worker_proc.poll() is not None: raise RuntimeError(f"S3 worker exited with code {s3_worker_proc.returncode}") if telegram_proc.poll() is not None: raise RuntimeError(f"Telegram worker exited with code {telegram_proc.returncode}") time.sleep(1) except KeyboardInterrupt: pass except RuntimeError as e: print(e, file=sys.stderr) finally: for proc in [telegram_proc, s3_worker_proc]: if proc and proc.poll() is None: proc.terminate() for proc in [telegram_proc, s3_worker_proc]: if proc and proc.poll() is None: try: proc.wait(timeout=10) except subprocess.TimeoutExpired: proc.kill()