archive
/
catfish
Archived
1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
catfish/catfish/worker/__init__.py

46 lines
878 B
Python

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