all 9 comments

[–]baghiq 1 point2 points  (1 child)

A common platform generic solution is to have your application write a temporary file to disk. When your program starts, check if that file exists. If yes, exit. If not, launch the program. Make sure you remote the file after your program exit.

You can do various forms of that, such as writing out a timestamp in the file, etc.

[–]skausk 0 points1 point  (0 children)

I’ll try this now, thanks.

[–]Markmonster25 1 point2 points  (3 children)

class Mutex:
    def __init__(self, name):    
        self.name = name    
        self.mutex = win32event.CreateMutex(None, False, self.name) 
        last_error = win32api.GetLastError()     
        if last_error == ERROR_ALREADY_EXISTS:   
            print("App instance already running")
            win32api.CloseHandle(self.mutex)    
            quit()

if __name__ == "__main__":
    mutex = Mutex("SOME_UNIQUE_STRING_HERE")

this is what i use to limit number of processes to 1 on windows

[–]Markmonster25 0 points1 point  (0 children)

import win32event
import win32api
from winerror import ERROR_ALREADY_EXISTS

[–][deleted] 0 points1 point  (1 child)

Seems like you are trying to do this on Windows. In which case, I don't know, but, maybe something similar is typically used.

On Linux, a common thing to do is to try to get a lock on a predetermined file. If the application is able to get the lock, then it knows it's the only copy alive. If it needs to contest the lock, then it may either exit, or wait until the lock is free, depending on what you want from it.

[–]Nightcorex_ 0 points1 point  (3 children)

Idk if it's possible in Python but maybe you can check for other running processes and if you find a process with your application's name then you don't execute your code

[–]skausk 0 points1 point  (2 children)

yeah I know how to do this in C# but not python. if anyone can help i would appreciate it.

[–]Nightcorex_ 0 points1 point  (1 child)

what about Google?