avoid script running twice
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Jun 18 21:54:00 EDT 2007
En Mon, 18 Jun 2007 16:30:05 -0300, Nick Craig-Wood <nick at craig-wood.com>
escribió:
> Tim Williams <tim at tdw.net> wrote:
>> You can also do this by holding a file open in write mode until the
>> script has finished.
>>
>> try:
>> open('lock.txt','w')
>> my_script()
>> except:
>> #print script is already running
>
> That only works under windows
Neither. The same file may be opened many times for writing at the same
time, from the same or different processes, even on Windows. So this alone
cannot be used as a locking mechanism.
> >>> f=open('lock.txt','w')
> >>> g=open('lock.txt','w')
> >>> f.write('hi')
> >>> g.write('ho')
> >>> f.close()
> >>> g.close()
> >>> open('lock.txt').read()
> 'ho'
The same happens on Windows. A file *can* be opened with exclusive access,
but this is not exposed thru the open() function in Python; one should use
the CreateFile Win32 API function with adequate flags.
--
Gabriel Genellina
More information about the Python-list
mailing list