check instace already running...

F

Fabio Pliger

Hi,
is it possibile, in python, to check for an already running instance of an
application?
My problem is that, if my program i running and the user relaunch it, i
don't want to open a new instance and have to instances of the same program
running togheter...
Can someone help me on this?
Fabio P.
 
S

Sidharth Kuruvila

I haven't tested this. There is probably a better way of doing this
looking at process information. I use a lock file to mark that the
program is already running. The problem is that for an abrupt shutdown
the file might not be removed.

import atexit
if os.path.exists(lockfile):
print "there is an instance already running"
else:
file(lockfile, "w").close()
atexit.register(lambda:eek:s.remove(lockfile))

//Your code here
 
A

Aldric L'Hernault

Sidharth Kuruvila a écrit :
I haven't tested this. There is probably a better way of doing this
looking at process information. I use a lock file to mark that the
program is already running. The problem is that for an abrupt shutdown
the file might not be removed.

To enhance your check, just write the PID into the file.
On startup, if file exists, you may check that the process
who has written it is still alive.

Aldric L.
 
F

Fabio Pliger

Aldric L'Hernault said:
Sidharth Kuruvila a écrit :

To enhance your check, just write the PID into the file.
On startup, if file exists, you may check that the process
who has written it is still alive.

Aldric L.


Yeah, but how can i retrieve my PID number?And how do i check if the process
who has written the file is still alive?If there a way to have the list of
the precesses running?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top