forked from ArchipelagoMW/Archipelago
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core, WebHost: lazy-load worlds in unpickler, WebHost and WebHostLib (A…
…rchipelagoMW#2156) * Core: lazy-load worlds in unpickler this should hopefully fix customserver's memory consumption * WebHost: move imports around to save memory in MP * MultiServer: prefer loading _speedups without pyximport This saves ~15MB per MP and speeds up module import if it was built in-place. * Tests: fix tests for changed WebHost imports * CustomServer: run GC after setup * CustomServer: cleanup exception handling
- Loading branch information
1 parent
2429140
commit 5fde565
Showing
8 changed files
with
93 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import sys | ||
|
||
|
||
class CommonLocker: | ||
"""Uses a file lock to signal that something is already running""" | ||
lock_folder = "file_locks" | ||
|
||
def __init__(self, lockname: str, folder=None): | ||
if folder: | ||
self.lock_folder = folder | ||
os.makedirs(self.lock_folder, exist_ok=True) | ||
self.lockname = lockname | ||
self.lockfile = os.path.join(self.lock_folder, f"{self.lockname}.lck") | ||
|
||
|
||
class AlreadyRunningException(Exception): | ||
pass | ||
|
||
|
||
if sys.platform == 'win32': | ||
class Locker(CommonLocker): | ||
def __enter__(self): | ||
try: | ||
if os.path.exists(self.lockfile): | ||
os.unlink(self.lockfile) | ||
self.fp = os.open( | ||
self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) | ||
except OSError as e: | ||
raise AlreadyRunningException() from e | ||
|
||
def __exit__(self, _type, value, tb): | ||
fp = getattr(self, "fp", None) | ||
if fp: | ||
os.close(self.fp) | ||
os.unlink(self.lockfile) | ||
else: # unix | ||
import fcntl | ||
|
||
|
||
class Locker(CommonLocker): | ||
def __enter__(self): | ||
try: | ||
self.fp = open(self.lockfile, "wb") | ||
fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) | ||
except OSError as e: | ||
raise AlreadyRunningException() from e | ||
|
||
def __exit__(self, _type, value, tb): | ||
fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN) | ||
self.fp.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters