473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Search within running python scripts

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
6 1113
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
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

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
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
In article <11**********************@p79g2000cwp.googlegroups .com>,
Simon Forman <ro*********@yahoo.comwrote:
>gmax2006 wrote:
Jul 25 '06 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rolfe | last post by:
Hi, I struggled, and got mod_python running on Apache/Win2k. Follow these instructions verbatim and you shouldn't have any trouble. These instructions are based on...
4
by: Peter Otten | last post by:
Is there a way to limit both width and height of a canvas text item? My current workaround seems clumsy: import Tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=400, height=200,...
3
by: Peter Schwalm | last post by:
I'd like to modify the python search path depending on the source directory of the script being started. The reason is: I use a version control system, and the python scripts and modules are...
3
by: Jim Britain | last post by:
I know absolutely nothing about Python. My background is shell scripts assembly language and C programming. Currently I work network support. This is a portion of a Python script written by ...
1
by: rshepard | last post by:
My editor is emacs in linux, and I have the python mode enabled. The two menus -- IM-Python and Python -- allow me to navigate within the loaded module and open execute buffers, among other things....
6
by: Ishpeck | last post by:
I'm using Python to automate testing software for my company. I wanted the computers in my testing lab to automatically fetch the latest version of the python scripts from a CVS repository and...
24
by: Mark | last post by:
Hi, I'm new to python and looking for a better idiom to use for the manner I have been organising my python scripts. I've googled all over the place about this but found absolutely nothing. I'm...
5
by: grt | last post by:
I'm new to python, running it under cygwin. This must be easy but I haven't figured it out. I'm trying to run a script by typing: python test.py where test.py is in a directory I've added to...
2
by: rodmc | last post by:
I am new to using Python as a CGI platform, so please excuse me if this is a dumb question. Anyway I have written a series of web forms (with Python scripts) which allow the user input but also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.