[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

how to start a python script only once

News123

3/13/2010 6:46:00 PM

Hi,

I'd like to make sure, that a certain python program will only be run
once per host. (linux/windows)


so if the program is started a second time it should just terminate and
let the other one run.

This does not have to be the fastest solution, but it should be reliable.


I have a few ideas, but wonder, which one is the most common


My ideas so far:

pid file and file locking
--------------------------
create a file like program.pid with the pid of the running program an
use file locking to avoid race connditions.

However I currently don't know how to do file locking under windows
and I don't know how to do file lockng with python and linux.
I'll start googling.

sqlite and locking
--------------------
quite some time ago I used a mysql table and locking as an inter-host mutex.

Perhaps sqlite would be good enough for an inter process mutex for
processes on the same host, but I don't know it well enough.


interprocess mutex
--------------------
well I even don't know whether something like this exists on linux / windows


Thanks in advanced for any tips



N
7 Answers

Francesco Bochicchio

3/14/2010 8:52:00 AM

0

On 13 Mar, 19:45, News123 <news1...@free.fr> wrote:
> Hi,
>
> I'd like to make sure, that a certain python program will only be run
> once per host. (linux/windows)
>
> so if the program is started a second time it should just terminate and
> let the other one run.
>
> This does not have to be the fastest solution, but it should be reliable.
>
> I have a few ideas, but wonder, which one is the most common
>
> My ideas so far:
>
> pid file and file locking
> --------------------------
> create a file like program.pid  with the pid of the running program an
> use file locking to avoid race connditions.
>
> However I currently don't know how to do file locking under windows
> and I don't know how to do file lockng with python and linux.
> I'll start googling.
>
> sqlite and locking
> --------------------
> quite some time ago I used a mysql table and locking as an inter-host mutex.
>
> Perhaps sqlite would be good enough for an inter process mutex for
> processes on the same host, but I don't know it well enough.
>
> interprocess mutex
> --------------------
> well I even don't know whether something like this exists on linux / windows
>
> Thanks in advanced for any tips
>
> N

Apart from file, a portable solution would be to bind to an unused
porta and assume that finding the port busy means that your program is
already running on the port.

On recent python installations there is the multiprocessing module
which provides process-level semaphores, but I don't know how portable
they are.

Ciao
----
FB

Daniel Fetchinson

3/14/2010 10:49:00 AM

0

> I'd like to make sure, that a certain python program will only be run
> once per host. (linux/windows)
>
>
> so if the program is started a second time it should just terminate and
> let the other one run.
>
> This does not have to be the fastest solution, but it should be reliable.
>
>
> I have a few ideas, but wonder, which one is the most common
>
>
> My ideas so far:
>
> pid file and file locking
> --------------------------
> create a file like program.pid with the pid of the running program an
> use file locking to avoid race connditions.
>
> However I currently don't know how to do file locking under windows
> and I don't know how to do file lockng with python and linux.
> I'll start googling.

This is the variant I'm using frequently too and I'd recommend to you as well.
Simple to implement and largely problem-free.

Cheers,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com...

News123

3/14/2010 2:30:00 PM

0

Hi Francesco,

Francesco Bochicchio wrote:
> On 13 Mar, 19:45, News123 <news1...@free.fr> wrote:
>> Hi,
>>
>> I'd like to make sure, that a certain python program will only be run
>> once per host. (linux/windows)
>>
>> so if the program is started a second time it should just terminate and
>> let the other one run.
>>
>> This does not have to be the fastest solution, but it should be reliable.
>>
>> I have a few ideas, but wonder, which one is the most common
>>
>> My ideas so far:
>>
>> pid file and file locking
>> --------------------------
>> create a file like program.pid with the pid of the running program an
>> use file locking to avoid race connditions.
>>
>> However I currently don't know how to do file locking under windows
>> and I don't know how to do file lockng with python and linux.
>> I'll start googling.
>>
>> sqlite and locking
>> --------------------
>> quite some time ago I used a mysql table and locking as an inter-host mutex.
>>
>> Perhaps sqlite would be good enough for an inter process mutex for
>> processes on the same host, but I don't know it well enough.
>>
>> interprocess mutex
>> --------------------
>> well I even don't know whether something like this exists on linux / windows
>>
>> Thanks in advanced for any tips
>>
>> N
>
> Apart from file, a portable solution would be to bind to an unused
> porta and assume that finding the port busy means that your program is
> already running on the port.
Yes, this should work.
I assume in this case the bind() function would handle the race condition.
>
> On recent python installations there is the multiprocessing module
> which provides process-level semaphores, but I don't know how portable
> they are.

Yes, there are process level semaphores in multiprocesing, but if I
understood well, they would either require a common parent process or
one process, which were acting as a manager, but then the question would
be how to make sure, that the manager process were started only once. (
bind port? )

News123

3/14/2010 2:32:00 PM

0

Hi Daniel,

Daniel Fetchinson wrote:
>> I'd like to make sure, that a certain python program will only be run
>> once per host. (linux/windows)
>>
>>
>> so if the program is started a second time it should just terminate and
>> let the other one run.
>>
>> This does not have to be the fastest solution, but it should be reliable.
>>
>>
>> I have a few ideas, but wonder, which one is the most common
>>
>>
>> My ideas so far:
>>
>> pid file and file locking
>> --------------------------
>> create a file like program.pid with the pid of the running program an
>> use file locking to avoid race connditions.
>>
>> However I currently don't know how to do file locking under windows
>> and I don't know how to do file lockng with python and linux.
>> I'll start googling.
>
> This is the variant I'm using frequently too and I'd recommend to you as well.
> Simple to implement and largely problem-free.

OK, So I'll just have to look in a piece of code, that performs file
locking for windows AND for linux and I should be fine.

bye

N

News123

3/14/2010 3:04:00 PM

0

Hi Daniel,


One more question:


Daniel Fetchinson wrote:
>> I'd like to make sure, that a certain python program will only be run
>> once per host. (linux/windows)
>>
>>
>> so if the program is started a second time it should just terminate and
>> let the other one run.
>>
>> This does not have to be the fastest solution, but it should be reliable.
>>
>>
>> I have a few ideas, but wonder, which one is the most common
>>
>>
>> My ideas so far:
>>
>> pid file and file locking
>> --------------------------
>> create a file like program.pid with the pid of the running program an
>> use file locking to avoid race connditions.
>>
>> However I currently don't know how to do file locking under windows
>> and I don't know how to do file lockng with python and linux.
>> I'll start googling.
>
> This is the variant I'm using frequently too and I'd recommend to you as well.
> Simple to implement and largely problem-free.
>
How do you make sure, that pid which is stored in the pid file does
really belong to the correct process and not just to any process, that
has by coincidence the same pid?


bye

N

News123

3/14/2010 3:04:00 PM

0

Hi Daniel,


One more question:


Daniel Fetchinson wrote:
>> I'd like to make sure, that a certain python program will only be run
>> once per host. (linux/windows)
>>
>>
>> so if the program is started a second time it should just terminate and
>> let the other one run.
>>
>> This does not have to be the fastest solution, but it should be reliable.
>>
>>
>> I have a few ideas, but wonder, which one is the most common
>>
>>
>> My ideas so far:
>>
>> pid file and file locking
>> --------------------------
>> create a file like program.pid with the pid of the running program an
>> use file locking to avoid race connditions.
>>
>> However I currently don't know how to do file locking under windows
>> and I don't know how to do file lockng with python and linux.
>> I'll start googling.
>
> This is the variant I'm using frequently too and I'd recommend to you as well.
> Simple to implement and largely problem-free.
>
How do you make sure, that pid which is stored in the pid file does
really belong to the correct process and not just to any process, that
has by coincidence the same pid?


bye

N

Glazner

3/14/2010 7:31:00 PM

0

On Mar 13, 8:45 pm, News123 <news1...@free.fr> wrote:
> Hi,
>
> I'd like to make sure, that a certain python program will only be run
> once per host. (linux/windows)
>
> so if the program is started a second time it should just terminate and
> let the other one run.
>
> This does not have to be the fastest solution, but it should be reliable.
>
> I have a few ideas, but wonder, which one is the most common
>
> My ideas so far:
>
> pid file and file locking
> --------------------------
> create a file like program.pid  with the pid of the running program an
> use file locking to avoid race connditions.
>
> However I currently don't know how to do file locking under windows
> and I don't know how to do file lockng with python and linux.
> I'll start googling.
>
> sqlite and locking
> --------------------
> quite some time ago I used a mysql table and locking as an inter-host mutex.
>
> Perhaps sqlite would be good enough for an inter process mutex for
> processes on the same host, but I don't know it well enough.
>
> interprocess mutex
> --------------------
> well I even don't know whether something like this exists on linux / windows
>
> Thanks in advanced for any tips
>
> N

I'll just open a port with a TCP socket, it is cross-platform and free
from race conditions.