Python Forum

Full Version: Python3x running only with one instance (how can I prevent too many running instance)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i tried to solve this problem with socket module but I couldn't. do you know useful efficent solution to solve this problem ? thanks.

example: when users reopened my program, it will give "program has already running please close other instance" error and will be closed.
A lockfile. Or, if you like very easy things, tendo.singleton.SingleInstance() https://github.com/pycontribs/tendo/blob...ngleton.py
(Jul-19-2017, 07:59 PM)nilamo Wrote: [ -> ]A lockfile.  Or, if you like very easy things, tendo.singleton.SingleInstance() https://github.com/pycontribs/tendo/blob...ngleton.py


i cant' trust lockfile because this file can be deleted from other users. i heard mutex in c/c++ language to do this. does mutex avaliable in python ?

i never seen singleton before. I'll try thanks.
The example is also using a lockfile inside a temporary directory.

A stupid solution can be to seek for all processes with the name 'python.exe' and look if the script is in cmdline.
The module psutil seems good use for it: http://pythonhosted.org/psutil/#psutil.process_iter

import sys
import psutil
import time

procs = [p for p in psutil.process_iter() if 'python.exe' in p.name() and __file__ in p.cmdline()]
if len(procs) > 1:
    print('Process is already running...')
    sys.exit(1)


for i in range(10):
    print('Running...')
    time.sleep(2)
But it's still possible to run the program twice, if you rename the python.exe or the script itself.
The lockfile should be the best solution for it. When the user deletes the lockfile, he deserves
everything what happens to his computer :-D
(Jul-21-2017, 09:58 AM)DeaD_EyE Wrote: [ -> ]The example is also using a lockfile inside a temporary directory.

A stupid solution can be to seek for all processes with the name 'python.exe' and look if the script is in cmdline.
The module psutil seems good use for it: http://pythonhosted.org/psutil/#psutil.process_iter

import sys
import psutil
import time

procs = [p for p in psutil.process_iter() if 'python.exe' in p.name() and __file__ in p.cmdline()]
if len(procs) > 1:
    print('Process is already running...')
    sys.exit(1)


for i in range(10):
    print('Running...')
    time.sleep(2)
But it's still possible to run the program twice, if you rename the python.exe or the script itself.
The lockfile should be the best solution for it. When the user deletes the lockfile, he deserves
everything what happens to his computer :-D

thanks very much. perhaps you played too many pc games. some pc games do this one instance app process perfectly. for exapmple. I played FIFA 2005 football game too many years ago. when this game be opened, I couldn't open another instance of this game. I made copy of FIFA 2005 for opening the another instance but when I tried to open cloned game, game not opened again. two same games on different location gave "program has already running" error. how did these games on diffirent location give this error ?

and too many another games can do it. examples: League of Legends, Dota2, FIFA games, .....

I think these games use sockets, ports. when another (second) instance of one of this games be opened, it begins to scan ports of operation system and if it finds first instance's port, gives error. but if it couldn't find any port of first instance, doesn't give any error and starts the first instance.
(Jul-20-2017, 04:10 PM)harun2525 Wrote: [ -> ]
(Jul-19-2017, 07:59 PM)nilamo Wrote: [ -> ]A lockfile.  Or, if you like very easy things, tendo.singleton.SingleInstance() https://github.com/pycontribs/tendo/blob...ngleton.py


i cant' trust lockfile because this file can be deleted from other users. i heard mutex in c/c++ language to do this. does mutex avaliable in python ?

i never seen singleton before. I'll try thanks.

If a user wants to run it twice, and they're willing to delete a lockfile to accomplish it, why not just let them? At that point, they know they could have undefined behavior.

If you try the port path, but use a port that some other program out there in the world is also using, then your program might never be able to run.

If you go with check the filename from psutil, then that doesn't stop someone from creating a wrapper script and just importing your program, so the "running program" has a different name. And then there's the fact that "python.exe" is only on windows, and won't stop someone from just renaming the python binary so that your program is running through "my-personal-python-v3.6.exe" or something.

There WAS a mutex module in python, but it was removed in 3.0 (and was deprecated since 2.6): https://docs.python.org/2/library/mutex....dule-mutex

I think using a lockfile would be the easiest way, since a determined user could just circumvent your checks anyway.