diff --git a/cromulant/main.py b/cromulant/main.py index 70250d9..5c5b5a4 100644 --- a/cromulant/main.py +++ b/cromulant/main.py @@ -1,5 +1,11 @@ from __future__ import annotations +import os +import sys +import fcntl +import tempfile +from pathlib import Path + from .config import Config from .utils import Utils from .ants import Ants @@ -11,6 +17,24 @@ from .filter import Filter def main() -> None: Config.prepare() + + program = Config.manifest["program"] + title = Config.manifest["title"] + + pid = f"{program}.pid" + pid_file = Path(tempfile.gettempdir(), pid) + fp = pid_file.open("w", encoding="utf-8") + + try: + fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + Utils.print(f"{title} is already running.") + sys.exit(0) + + # Create singleton + fp.write(str(os.getpid())) + fp.flush() + Utils.prepare() Window.prepare() Ants.prepare()