Re: Making sure script only runs once instance at a time.

2006-10-07 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Tim Williams wrote: > On 29/09/06, Matthew Warren <[EMAIL PROTECTED]> wrote: >> I have found that in real usage of other programs within the company that >> use lockfiles, it sometimes causes a bit of troubleshooting time when it >> stops working due to a stale lock

Re: Making sure script only runs once instance at a time.

2006-10-03 Thread Eric S. Johansson
MonkeeSage wrote: > Eric S. Johansson wrote: >> the problem with this solution is that it does not handle the read >> nonexclusive/write exclusive locking model. In this model, reads don't >> block, they only register that the request is in process. writes lock >> request block until all outstand

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread MonkeeSage
Eric S. Johansson wrote: > the problem with this solution is that it does not handle the read > nonexclusive/write exclusive locking model. In this model, reads don't > block, they only register that the request is in process. writes lock > request block until all outstanding reads have completed

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Eric S. Johansson
MonkeeSage wrote: > Here's a class using Fredrik's suggestions to provide generic, > cross-platform file locking (only tested on *nix however, with the two > test files listed [i.e., run test1.py in one terminal then test2.py in > another]): > > http://pastie.caboo.se/15851 > > Ps. The lockfile s

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread MonkeeSage
Here's a class using Fredrik's suggestions to provide generic, cross-platform file locking (only tested on *nix however, with the two test files listed [i.e., run test1.py in one terminal then test2.py in another]): http://pastie.caboo.se/15851 Ps. The lockfile should also always be cleaned up (e

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon
The name thing is taken care of by the fact that I use the path not just the script name. The path appears in the command and then is grepped out by that criteria, so if there was another program with the same name and in a different path then it would not affect it. Of course this is defeatab

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Fredrik Lundh
Hari Sekhon wrote: > How exactly do you check that the pid is still active in python? Is > there a library or something that will allow me to manipulate system > processes and listings etc the way everybody does in unix shells by passing zero to the os.kill primitive: os.kill(pid,

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon
AMENDMENT: The line number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc -l' % scriptpath) was supposed to be number_procs=int(commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc -l' % scriptpath)[1]) -h Hari Sekhon Hari Sekhon wrote: Fredrik Lundh wrote:

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon
Fredrik Lundh wrote: Hari Sekhon wrote: I'm not sure if that is a very old way of doing it, which is why I was reluctant to do it. My way actually uses the process list of the os (linux) and counts the number of instances. If it is more than 0 then another process is running an

RE: Making sure script only runs once instance at a time.

2006-09-30 Thread Matthew Warren
Apologies for repost. not sure what happened.   This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. You should not copy the email, use it for any purpose or disclose its contents

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Paul Rubin
Fredrik Lundh <[EMAIL PROTECTED]> writes: > opening a file for writing doesn't lock it on Unix. if you want > locking, you need other mechanisms, and I'm not sure any of them > work properly on all configurations, under all circumstances. Hmm. What about trying to listen on a PF_UNIX socket asso

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Tim Williams wrote: > > > So that I know my mistake, which bit fails (the text from > > sys.exc_info()[1]?? ) , or is it all complete rubbish - and not > > do-able - on a *nix system ? > > opening a file for writing doesn't lock it on Unix.

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Fredrik Lundh
Paul Rubin wrote: >> That's the same kind of principle as my posted snippet, it doesn't >> rely on the file's presence or absence as such. It only cares when >> the file exists *and* is held open by another process.When the >> process exits, the lock ends without needing to clean up. >> >>

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29 Sep 2006 10:04:15 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote: > "Tim Williams" <[EMAIL PROTECTED]> writes: > > That's the same kind of principle as my posted snippet, it doesn't > > rely on the file's presence or absence as such. It only cares when > > the file exists *and* is

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Fredrik Lundh
Tim Williams wrote: > So that I know my mistake, which bit fails (the text from > sys.exc_info()[1]?? ) , or is it all complete rubbish - and not > do-able - on a *nix system ? opening a file for writing doesn't lock it on Unix. if you want locking, you need other mechanisms, and I'm not sure

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Fredrik Lundh
Paul Rubin wrote: > Don't use the presence or absence of a file as a lock. Have the file > there all the time, and have the app open it and use fcntl to get an > exclusive kernel lock on the file descriptor. doesn't work on all file systems, though... -- http://mail.python.org/mailman/listin

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Paul Rubin
"Tim Williams" <[EMAIL PROTECTED]> writes: > That's the same kind of principle as my posted snippet, it doesn't > rely on the file's presence or absence as such. It only cares when > the file exists *and* is held open by another process.When the > process exits, the lock ends without needing

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29 Sep 2006 09:47:12 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote: > "Tim Williams" <[EMAIL PROTECTED]> writes: > > My reply was in response to a post that mentioned a known problem with > > this, what happens when the previously running program doesn't exit > > gracefully, and leav

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Paul Rubin
"Tim Williams" <[EMAIL PROTECTED]> writes: > My reply was in response to a post that mentioned a known problem with > this, what happens when the previously running program doesn't exit > gracefully, and leaves the directory in place ?? Don't use the presence or absence of a file as a lock. Have

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Sybren Stuvel <[EMAIL PROTECTED]> wrote: > Checking for a lock, and later acquiring the lock, seems non-atomic to > me. It's easier to simply create a lock directory. If it fails, the > dir already exists and someone else holds the lock. If it succeeds, > the lock is immediately yours,

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Sybren Stuvel
Tim Williams enlightened us with: > def check_lock(): > import os, sys > try: > os.remove({filename}) > except: > if "Permission denied" in sys.exc_info()[1]: > print 'This program is already running' > sys.exit() > f_lock = open({filename},'w

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Tim Williams wrote: > > > def check_lock(): > > import os, sys > > try: > > os.remove({filename}) > > except: > > if "Permission denied" in sys.exc_info()[1]: > > print 'This program is already running'

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Fredrik Lundh
Tim Williams wrote: > def check_lock(): > import os, sys > try: > os.remove({filename}) > except: > if "Permission denied" in sys.exc_info()[1]: > print 'This program is already running' > sys.exit() > f_lock = open({filename},'w') have you

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Matthew Warren <[EMAIL PROTECTED]> wrote: > I have > found that in real usage of other programs within the company that use > lockfiles, it sometimes causes a bit of troubleshooting time when it stops > working due to a stale lockfile.. This especially happens when the program > is ki

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Fredrik Lundh
Hari Sekhon wrote: > I'm not sure if that is a very old way of doing it, which is why I was > reluctant to do it. My way actually uses the process list of the os > (linux) and counts the number of instances. If it is more than 0 then > another process is running and the script exits gracefully.

RE: Making sure script only runs once instance at a time.

2006-09-29 Thread Matthew Warren
  From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hari SekhonSent: 29 September 2006 14:55To: python-list@python.orgSubject: Re: Making sure script only runs once instance at a time. I'm not sure if that is a very old way of doing it, which is

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Hari Sekhon
I'm not sure if that is a very old way of doing it, which is why I was reluctant to do it. My way actually uses the process list of the os (linux) and counts the number of instances. If it is more than 0 then another process is running and the script exits gracefully. Also, apart from the fact

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Paul Rubin
Hari Sekhon <[EMAIL PROTECTED]> writes: > Seeing as there doesn't seem to be a good answer to this (or at least > not one that we have so far some up with) I have decided to fall back > to my old friend the unix shell. It's as portable as python, but is > very flexible and fast at doing real things

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Hari Sekhon
Seeing as there doesn't seem to be a good answer to this (or at least not one that we have so far some up with) I have decided to fall back to my old friend the unix shell. It's as portable as python, but is very flexible and fast at doing real things and will tell me if another process by this

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread MaR
A very brutal but simple and effective method is to bind() to a socket on localhost eg (127.0.0.1, 4711), listen() but never accept(). Any other process trying to to bind() on the same port will fail.. When the process dies, the port is released automatically, pending som timedelay.. But this assu

Re: Making sure script only runs once instance at a time.

2006-09-28 Thread Stephan Kuhagen
Hari Sekhon wrote: > I have written a script and I would like to ensure that the script is > never run more than once at any given time. > > What is the best way of testing and exiting if there is another version > of this script running somewhere on this machine? > > I guess what I'm asking is

Re: Making sure script only runs once instance at a time.

2006-09-28 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Hari Sekhon wrote: > I have written a script and I would like to ensure that the script is > never run more than once at any given time. -- http://mail.python.org/mailman/listinfo/python-list

Re: Making sure script only runs once instance at a time.

2006-09-27 Thread Eric S. Johansson
Hari Sekhon wrote: > I have written a script and I would like to ensure that the script is > never run more than once at any given time. > > What is the best way of testing and exiting if there is another version > of this script running somewhere on this machine? > > I guess what I'm asking is

Making sure script only runs once instance at a time.

2006-09-27 Thread Hari Sekhon
I have written a script and I would like to ensure that the script is never run more than once at any given time. What is the best way of testing and exiting if there is another version of this script running somewhere on this machine? I guess what I'm asking is how to handle system processes t