Search within running python scripts

G

gmax2006

Hi,

Is it possible that a python script finds out whether another instance
of it is currently running or not?

Thank you,
Max
 
S

Simon Forman

gmax2006 said:
Hi,

Is it possible that a python script finds out whether another instance
of it is currently running or not?

Thank you,
Max

Yes, there are several ways. What OS are you using?

~Simon
 
F

faulkner

IPC via files, sockets, and shared memory are all readily available in
python.
the simplest way is to have the script write its pid to a certain file.

pidfn = '/tmp/hellowerld_ipc_pid'
if os.path.isfile(pidfn):
f = file(pidfn)
pid = f.read()
f.close()
if pid in os.popen('ps -A -o pid').read():
print "another instance of me is running!"
else:
f = file(pidfn, 'w')
f.write(str(os.getpid()))
f.close()
 
G

gmax2006

Simon said:
Yes, there are several ways. What OS are you using?

~Simon

I have to use an os-independent approach.

At this point I use a file as running-flag. It doesn't work so good.
Because if the python application breaks or get terminated, it won't
run again unless somebody deletes the flag file.

Alan
 
S

Simon Forman

gmax2006 said:
I have to use an os-independent approach.

At this point I use a file as running-flag. It doesn't work so good.
Because if the python application breaks or get terminated, it won't
run again unless somebody deletes the flag file.

Alan

Hmm, I'm very far from being an expert on this, so hopefully someone
who knows better will reply.
You might have to check the OS your script is running on and do, say,
what faulkner proposed for linux (and Mac?), and something like
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474070 for
windows.

HTH,
~Simon
 
C

Cameron Laird

gmax2006 wrote: .
.
.

Hmm, I'm very far from being an expert on this, so hopefully someone
who knows better will reply.
You might have to check the OS your script is running on and do, say,
what faulkner proposed for linux (and Mac?), and something like
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474070 for
windows.
.
.
.
Particularly when I hear "os-independent", I think first of
binding to a socket. While <URL: http://wiki.tcl.tk/1558 >
is written for a Tcl-based crowd, the commentary there ap-
plies quite well to Python.
 
S

Simon Forman

Cameron Laird wrote:
....
Particularly when I hear "os-independent", I think first of
binding to a socket. While <URL: http://wiki.tcl.tk/1558 >
is written for a Tcl-based crowd, the commentary there ap-
plies quite well to Python.

I was going to suggest something like this, as I have noticed that IDLE
seems to do exactly this, and on windows and linux, but I was afraid to
look the fool if it was indeed foolish. (and also, I didn't know
details of it.)

Thanks Cameron.

Peace,
~Simon
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top