From 853997bfd555c26a527c0969174c15903b93138d Mon Sep 17 00:00:00 2001 From: Wilson Gomez Date: Sat, 21 Oct 2023 11:18:07 -0500 Subject: [PATCH] restrict open application multiple times --- .gitignore | 2 +- INSTALL | 2 ++ INSTALL_es | 2 ++ pospro | 31 +++++++++++++++++++++++++++++-- setup.py | 4 +++- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 716a4f7..225553a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* -*.log* +*.log package-lock* diff --git a/INSTALL b/INSTALL index ca08a19..2d245a1 100644 --- a/INSTALL +++ b/INSTALL @@ -32,6 +32,8 @@ The following packages must be installed using PIP pip3 install orjson pip3 install escpos pip3 install PySide6 + pip3 install psutil + pip3 install setproctitle For Windows add: diff --git a/INSTALL_es b/INSTALL_es index d5718a7..40cc2de 100644 --- a/INSTALL_es +++ b/INSTALL_es @@ -34,6 +34,8 @@ Los siguientes paquetes se deben instalar usando PIP pip3 install orjson pip3 install escpos pip3 install PySide6 + pip3 install psutil + pip3 install setproctitle Tener en cuenta que algunos paquetes se deben instalar con pip para python3. diff --git a/pospro b/pospro index 49fc57f..3adba1b 100755 --- a/pospro +++ b/pospro @@ -3,10 +3,14 @@ import os import sys import argparse -from PySide6.QtWidgets import QApplication +import setproctitle +import psutil +from PySide6.QtCore import Qt, QCoreApplication +from PySide6.QtWidgets import QApplication, QMessageBox # from PyQt5.QtWidgets import QApplication from app.commons.dblogin import Login from app import main +from logger_config import logger parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('-o', metavar="theme", help='execute argument') @@ -20,15 +24,38 @@ try: except NameError: pass +PROCESS_NAME = "app_presik_pos" + + +class SingleInstanceApp(QApplication): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + for process in psutil.process_iter(['pid', 'name']): + if process.info['name'] == PROCESS_NAME: + logger.error('La aplicación ya esta en ejecución') + self.show_already_running_dialog() + sys.exit(1) + + def show_already_running_dialog(self): + msg = QMessageBox() + msg.setIcon(QMessageBox.Warning) + msg.setText("La aplicación ya está en ejecución.") + msg.setWindowTitle("Aplicación en ejecución") + msg.setStandardButtons(QMessageBox.Ok) + msg.exec() + class Client(object): def __init__(self, parent=None): - self.app = QApplication(sys.argv) + # self.app = QApplication(sys.argv) + self.app = SingleInstanceApp(sys.argv) self.app.setOrganizationName("PRESIK SAS") self.app.setOrganizationDomain("presik.com") self.app.setApplicationName("SMART POS") self.app.setStyle("fusion") + setproctitle.setproctitle(PROCESS_NAME) def init_login(self): _file_config = 'config_pos.ini' diff --git a/setup.py b/setup.py index 9bbc7a7..027673a 100644 --- a/setup.py +++ b/setup.py @@ -71,6 +71,8 @@ setup( 'paramiko', 'orjson', 'escpos', - 'PySide6>=6.4.1' + 'PySide6>=6.4.1', + 'psutil', + 'setproctitle' ] )