NFS를 통해 여러 VM이 액세스하는 서버에 results.txt
파일이 있습니다. 각 VM에서 프로세스가 실행되어 results.txt
파일을 읽고 수정합니다. A
과 B
의 두 프로세스가 동시에 파일을 읽으면 프로세스가 파일에 쓰는 순서에 따라 results.txt
에 A 또는 B 수정이 나타납니다.Python : NFS에서 텍스트 파일 잠그기
프로세스 A
에 파일에 대한 쓰기 잠금이있는 경우 B
프로세스는 잠금이 해제 될 때까지 대기하여 results.txt
파일을 읽어야합니다. 나는이 사용하는 파이썬을 구현하는 시도
:
로컬 디스크에있는 파일에 대해 예상대로 작동import fcntl
f = open("/path/result.txt")
fcntl.flock(f,fcntl.LOCK_EX)
#code
. 내가 fcntl.fcntl
및 fcntl.flock
을 시도했지만 같은 오류가 발생했습니다
Traceback (most recent call last):
File "lock.py", line 12, in <module>
fcntl.flock(f,fcntl.LOCK_EX)
IOError: [Errno 45] Operation not supported
: 나는 탑재 된 경로에서 파일을 잠금을 시도 실행할 때
하지만, 나는 다음과 같은 오류가 발생합니다. 이것이 fcntl
을 사용하는 방식에 문제가 있습니까? 파일이 저장된 서버에 구성이 필요합니까?
편집 :
이것은 내가 fcntl.fcntl
을 사용하고 방법입니다
f= open("results.txt")
lockdata = struct.pack('hhllhh', fcntl.F_RDLCK,0,0,0,0,0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
NFS 서버 버전은 내 요구 사항에 대한 flufl.lock이 가장 적합한 발견 3.
'fcntl.fcntl()'을 사용하는 방법을 붙여 줄 수 있습니까? – vmonteco
@vmonteco, 코드 스 니펫을 추가했습니다. – SilentMonk
'fcntl.fcntl (f, fcntl.LOCK_EX)'가 작동합니까? – vmonteco