0

I want to know if there is a pythonic way to prevent multiple instances of an application from running. Let's say if the application is already running then the user must not be able to launch the same application again.

Here is a sample PyQt5 code.

import sys
from PyQt5.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Using Labels")
        self.setGeometry(50,50,350,350)
        self.UI()


    def UI(self):
        text1=QLabel("Hello Python",self)
        text2=QLabel("Hello World",self)
        text1.move(50,50)
        text2.move(200,150)
        self.show()

def main():
    App = QApplication(sys.argv)
    window=Window()
    sys.exit(App.exec_())

if __name__ == '__main__':
    main()

I came across this solution which uses this tendo module/library. Though the answer works perfectly but I don't wanna install another dependency for such a trivial problem.

I'm developing my app for windows systems but would appreciate if the answer is for cross platform.

Voldemort
  • 175
  • 5
  • 20

1 Answers1

0

Nothing particularly "pythonic", but standard practice is to have a lock file: If it doesn't exist, you start up & create the file. If the file does exist, block startup.

That's what the tendo singleton.py code does (appears to do -- I've not tested it), so I'd suggest using tendo, or take a look at tendo/singleton.py

pbuck
  • 4,291
  • 2
  • 24
  • 36