Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Checking for duplicate instances of a script... (http://www.velocityreviews.com/forums/t327209-checking-for-duplicate-instances-of-a-script.html)

Guillaume Dargaud 01-21-2004 09:28 AM

Checking for duplicate instances of a script...
 
Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?
Besides checking 'ps' for its own name. Most modern langages have a direct
way to do that.
Thanks
--
Guillaume Dargaud
http://www.gdargaud.net/
"Here is your parachute and here is the manual.
Welcome to Linux."



Joakim Hove 01-21-2004 12:42 PM

Re: Checking for duplicate instances of a script...
 

"Guillaume Dargaud" <NOusenetSPAM@gdargaud.net> writes:

> Hello, python beginner here,
> How can I make sure that a given script is never run more than once at the
> same time in a system ?


This can probably be done in *many* different ways, maybe there is
even an agreed upon on best solution. This is a small suggestion
based on fcntl.flock() (Unix only I am afraid):

#!/usr/bin/python
import struct, fcntl
import sys
lockfile = "/tmp/.lock"

## Open a file-descriptor for the lock file.
lockH = open(lockfile,"w")

## Try to aquire an exclusive lock on the lock file.
try:
fcntl.flock(lockH , fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError,e:
sys.exit("Lockfile %s already locked - an instance is probably already running" % lockfile)


##
## Your code ...
##


## Release the lock again
fcntl.flock(lockH, fcntl.LOCK_UN)


--
/--------------------------------------------------------------------\
/ Joakim Hove / hove@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/

Guillaume Dargaud 01-21-2004 02:25 PM

Re: Checking for duplicate instances of a script...
 
> This can probably be done in *many* different ways, maybe there is
> even an agreed upon on best solution. This is a small suggestion
> based on fcntl.flock() (Unix only I am afraid):


Wrong prediction: it works fine in Cygwin under windows, but it doesn't on
my linux server...

Traceback (innermost last):
File "./CounterTest.py", line 24, in ?
fcntl.flock(lockH, fcntl.LOCK_EX | fcntl.LOCK_NB)
TypeError: illegal argument type for built-in operation


Cygwin:
2.3.3 (#1, Dec 30 2003, 08:29:25)
[GCC 3.3.1 (cygming special)]

Linux:
1.5.2 (#1, Jan 31 2003, 10:58:35) [GCC 2.96 20000731 (Red Hat Linux 7.3 2

Is this too old ?

Maybe I'll try with os.open flags...
--
Guillaume Dargaud
http://www.gdargaud.net/
"My goal is to be a meteorologist. But since I possess no training in
meteorology, I suppose I should try stock brokerage." - Found in a
resume.



William Park 01-21-2004 10:51 PM

Re: Checking for duplicate instances of a script...
 
Guillaume Dargaud <NOusenetSPAM@gdargaud.net> wrote:
> Hello, python beginner here,
> How can I make sure that a given script is never run more than once at the
> same time in a system ?
> Besides checking 'ps' for its own name. Most modern langages have a direct
> way to do that.


1. Check 'ps'. But, if 2 processes checks at the same, then they see
nothing is running and decides to run themselves.

2. Use lockfile.

--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing.

Dang Griffith 01-22-2004 12:46 PM

Re: Checking for duplicate instances of a script...
 
On Wed, 21 Jan 2004 10:28:20 +0100, "Guillaume Dargaud"
<NOusenetSPAM@gdargaud.net> wrote:

>Hello, python beginner here,
>How can I make sure that a given script is never run more than once at the
>same time in a system ?
>Besides checking 'ps' for its own name. Most modern langages have a direct
>way to do that.
>Thanks

What modern language has a direct way to do that?
--dang

Josiah Carlson 01-22-2004 04:43 PM

Re: Checking for duplicate instances of a script...
 
> >Hello, python beginner here,
> >How can I make sure that a given script is never run more than once at the
> >same time in a system ?
> >Besides checking 'ps' for its own name. Most modern langages have a direct
> >way to do that.
> >Thanks

> What modern language has a direct way to do that?


You can register a COM or CORBA interface (is this what they are called?)
and attempt to connect to it during startup. If you can connect to
yourself, don't start up, if you can't, start up.

- Josiah



All times are GMT. The time now is 04:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.