how to start a python script only once

News123 news1234 at free.fr
Sun Mar 14 11:04:25 EDT 2010


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



More information about the Python-list mailing list