forked from Quazer/anglerfish
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathset_single_instance.py
More file actions
32 lines (26 loc) · 1.08 KB
/
set_single_instance.py
File metadata and controls
32 lines (26 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Set process name and cpu priority,return socket.socket or None."""
import logging as log
import socket
import sys
def set_single_instance(name: str, port: int=8_888) -> socket.socket:
"""Set process name and cpu priority,return socket.socket or None."""
try: # Single instance app ~crossplatform, uses udp socket.
log.info("Creating Abstract UDP Socket Lock for Single Instance.")
__lock = socket.socket(
socket.AF_UNIX if sys.platform.startswith("linux")
else socket.AF_INET, socket.SOCK_STREAM)
__lock.bind(f"\0_{name}__lock" if sys.platform.startswith("linux")
else ("127.0.0.1", port))
except (socket.error, OSError, Exception) as e:
__lock = None
log.critical("Another instance of App is already running!, Exiting!.")
log.exception(e)
sys.exit()
exit()
0 / 0 # should never reach here.
else:
log.info(f"Socket Lock for 1 Single App Instance: {__lock},{__lock!r}")
finally:
return __lock