473,385 Members | 1,798 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,385 software developers and data experts.

testing if another instance of a script is already running

Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Sep 12 '08 #1
6 6345
Strato wrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Here is a recipe for Linux version of singleinstance class:

http://code.activestate.com/recipes/546512/

and I wrote and contributed the equivalent Windows version:

http://code.activestate.com/recipes/474070/

Hope this helps.

-Larry
Sep 13 '08 #2
Strato wrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Here is a recipe for Windows version of singleinstance class:

http://code.activestate.com/recipes/474070/

and I wrote and contributed the equivalent Linux version:

http://code.activestate.com/recipes/546512/

Hope this helps.

-Larry
Sep 13 '08 #3
On Sep 12, 7:08*am, Strato <pyt...@p0w.orgwrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
You could use msvcrt.locking, and just lock the script file. I am not
sure about this.
Sep 13 '08 #4
>I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process
control, but I'm a neebie and I don't see how to do this in a safe
and clean way.
AaronYou could use msvcrt.locking, and just lock the script file. I
Aaronam not sure about this.

If you want a cross-platform solution, you might try the lockfile module
instead:

http://pypi.python.org/pypi/lockfile

Skip
Sep 13 '08 #5
On Sep 13, 5:34*pm, s...@pobox.com wrote:
* * >I don't want to handle writing of a PID file because it is too
* * >Unix/Linux specific way to do this, and I need to keep the code to be
* * >cross-platform.
* * >>
* * >I think the better way to achieve this is to use some process
* * >control, but I'm a neebie and I don't see how to do this in a safe
* * >and clean way.

* * AaronYou could use msvcrt.locking, and just lock the script file. *I
* * Aaronam not sure about this.

If you want a cross-platform solution, you might try the lockfile module
instead:

* *http://pypi.python.org/pypi/lockfile

Skip
Would it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL ?
Would that be platform-independent?

Windows docs (_open):

_O_CREAT| _O_EXCL
Returns an error value if the file specified by filename exists.
Applies only when used with _O_CREAT.

man page:

O_EXCL
If O_EXCL and O_CREAT are set, open will fail if the file exists.
The check for the existence of the file and the creation of the file
if it does not exist is atomic with respect to other processes
executing open naming the same filename in the same directory with
O_EXCL and O_CREAT set.

CreateDirectory:

The CreateDirectory function does seem to guarantee this as well,
though is not as explicit:

Possible errors include the following.
ERROR_ALREADY_EXISTS
The specified directory already exists.

But the lock operation wouldn't need the 'if hasattr(os, "link")' test.
Sep 14 '08 #6

AaronWould it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL
Aaron? Would that be platform-independent?

I suspect it would be platform-independent but not NFS-safe. (The other
solutions in lockfile might have NFS problems as well. In any case,
lockfile provides a bit more functionality as well, including timeouts and
the ability to break locks.

Skip
Sep 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pierre-Yves | last post by:
Hello, I would like to prevent my perl program to be executed several times simultaneously (if the program is already running, I would like to display a message like "another instance of this...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
1
by: Kevin Otte | last post by:
Hi, I'm currently working on a program that is supposed to allow only one instance of itself. How can I transfer the data that is passed to a second instance as a parameter to the first...
4
by: CliffKing | last post by:
I am trying to use Windows Task Scheduler to run a batch file for an already open MS Access database. Below is the syntax to the batch file: Batch file: DailySalesExport.bat REM This runs the...
25
by: acemtp | last post by:
Hello, I would like to show you a new collaborative website for javascript programmer. It's quite hard to know if some specific code you wrote will work on all browsers, and if not, why it...
2
by: pamela fluente | last post by:
I have an application running. A file type is registered with this application. When the user click on a file of such type a new instance of the application is loaded with command line (file name)....
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
7
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try...
0
by: Tim Golden | last post by:
Tim Golden wrote: Sorry, not the best of links to point to. Basically, search mail.python.org for things like "CreateMutex" and "single application instance". TJG
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...

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.