Skip to content

Instantly share code, notes, and snippets.

@loathingKernel
Created December 13, 2023 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loathingKernel/53cf9a9a855c8d701e6783ff604a315e to your computer and use it in GitHub Desktop.
Save loathingKernel/53cf9a9a855c8d701e6783ff604a315e to your computer and use it in GitHub Desktop.
import os
import sys
import time
from filelock import FileLock
from PyQt5.QtCore import QRunnable, QThreadPool, QTimer, pyqtSlot
from PyQt5.QtWidgets import QApplication
class LockRunnable(QRunnable):
def __init__(self, lock: FileLock, inst: int):
super().__init__()
self.setAutoDelete(True)
self.lock = lock
self.inst = inst
def lock_installed(self) -> bool:
"""
Locks the install data. We do not care about releasing this lock.
If it is acquired by a Legendary instance it should own the lock until it exits.
Some operations such as egl sync may be simply skipped if a lock cannot be acquired
"""
if self.lock.is_locked:
return True
try:
self.lock.acquire(blocking=False)
# reload data in case it has been updated elsewhere
try:
print("load installed.json")
except Exception as e:
print(f'Failed to load installed game data: {e!r}')
return True
except TimeoutError:
return False
def run(self):
if not self.lock_installed():
print("Fail")
else:
print("Continue")
time.sleep(1)
class App(QApplication):
def __init__(self):
super().__init__(sys.argv)
self.lock = FileLock(
os.path.join(os.path.expanduser("~/"), 'filelock_legendary_poc') + '.lock',
thread_local=False
)
self.threadpool = QThreadPool.globalInstance()
QTimer.singleShot(0, self.start)
@pyqtSlot()
def start(self):
for i in range(10):
r = LockRunnable(self.lock, i)
self.threadpool.start(r)
self.threadpool.waitForDone()
self.quit()
if __name__ == "__main__":
app = App()
sys.exit(app.exec())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment