Heads up! This post was written 13 years ago. Some information might be outdated or may have changed since then.
Решението:
import os, psutil
def write_pidfile_or_die(path_to_pidfile):
if os.path.exists(path_to_pidfile):
pid = int(open(path_to_pidfile).read())
if psutil.pid_exists(pid):
raise SystemExit
else:
os.remove(path_to_pidfile)
open(path_to_pidfile, 'w').write(str(os.getpid()))
return path_to_pidfile
temp_dir = os.environ['APPDATA'] + '/SOME_DIR'
write_pidfile_or_die(temp_dir+'/pidfun.pid') като идеята е следната: при стартиране на приложението проверяваме файла и PID-а дали съществува, ако съществува SystemExit, ако не съществува записваме сегашния pid