File locking

D

Dr. Who

I'm always disappointed when I find something that Python doesn't
handle in a platform independent way. It seems to me that file
locking is in that boat.

1. I don't see a way to atomically open a file for writing if and only
if it doesn't exist without resorting to os.open and specialized
platform O_XXX flags.

2. I don't see a way to atomically open a file for writing and obtain
a lock on the file.

3. I don't see a platform independent way to obtain a lock on a file.
You have to do something goofy like
if sys.platform == win32:
import msvcrt
else:
import fcntl
and then similar things to call the correct functions with the correct
flags.

Please let me know if I'm missing something since they seem like
normal file operations that I would hope Python would abstract away.
If not, are there any PEPs concerning this for Python3K?

Thanks,
Jeff
 
M

Marc 'BlackJack' Rintsch

Please let me know if I'm missing something since they seem like
normal file operations that I would hope Python would abstract away.

I don't think of them as "normal" operations. They are "extra" stuff that
is not only dependent on the operating system but also on the file system
in use.

Ciao,
Marc 'BlackJack' Rintsch
 
J

John Nagle

Marc said:
I don't think of them as "normal" operations. They are "extra" stuff that
is not only dependent on the operating system but also on the file system
in use.

It's a UNIX legacy problem. UNIX historically was very weak
at interprocess communication and locking, and while today, most
UNIX-like systems have some kind of add-on for those functions,
they were never really standardized.

But see

http://www.thinkspot.net/scripts/posixmutexfile.py
http://www.thinkspot.net/scripts/mutexfile.py
http://www.thinkspot.net/scripts/winmutexfile.py

for an attempt to deal with this probl;em in a portable way.
The code is somewhat dated, but it's on the right track.

John Nagle
 
D

Dennis Lee Bieber

Please let me know if I'm missing something since they seem like
normal file operations that I would hope Python would abstract away.
If not, are there any PEPs concerning this for Python3K?
My experience has been on other machines than UNIX/Linux... On
practically everything I've had more than a smattering of use, getting
files to "share" has been the hard part, not the locking.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top