import asyncio import os import tempfile import time import psutil from catfish.router import run_server from catfish.utils.processes import terminate_processes PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid") def is_running(): return os.path.exists(PID_FILE) def get_running_process() -> psutil.Process: assert is_running() with open(PID_FILE) as f: return psutil.Process(int(f.read())) def wait_for_worker(): while True: try: get_running_process() return except ValueError: pass time.sleep(0.1) def stop_worker(): if is_running(): terminate_processes([get_running_process()]) async def run_worker(port): loop = asyncio.get_running_loop() await asyncio.gather(run_server(loop, port)) def run(port=8080): return asyncio.run(run_worker(port))