By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Search within running python scripts

P: n/a
Hi,

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

Thank you,
Max

Jul 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
gmax2006 wrote:
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

Jul 24 '06 #2

P: n/a
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()
gmax2006 wrote:
Hi,

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

Thank you,
Max
Jul 24 '06 #3

P: n/a

Simon Forman wrote:
gmax2006 wrote:
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
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

Jul 24 '06 #4

P: n/a
gmax2006 wrote:
Simon Forman wrote:
gmax2006 wrote:
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

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/Coo.../Recipe/474070 for
windows.

HTH,
~Simon

Jul 24 '06 #5

P: n/a
In article <11**********************@p79g2000cwp.googlegroups .com>,
Simon Forman <ro*********@yahoo.comwrote:
>gmax2006 wrote:
Jul 25 '06 #6

P: n/a
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

Jul 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.