python multi thread to process file with fcntl flcok
Question: I try to use python to handle text replace problem. There is a file of Little-
endian UTF-16 format, I want to replace the ip address in this file. First, I
read this file by line, then replace the target string, last, I write the new
string to the file. But with multi thread operate this file, the file will be
garbled. Here is my code.

    
    
    import re
    import codecs 
    import time
    import thread
    import fcntl
    
    ip = "10.200.0.1" 
    searchText = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" 
    
    def replaceFileText(fileName,searchText,replaceText,encoding):
        lines = []
        with codecs.open(fileName,"r",encoding) as file:
            fcntl.flock(file,fcntl.LOCK_EX)
            for line in file:
                lines.append(re.sub(searchText,replaceText,line))
            fcntl.flock(file,fcntl.LOCK_UN)
    
        with codecs.open(fileName,"w",encoding) as file:
            fcntl.flock(file,fcntl.LOCK_EX)
            for line in lines:
                file.write(line)
            fcntl.flock(file,fcntl.LOCK_UN)
    
    def start():
        replaceFileText("rdpzhitong.rdp",searchText,ip,"utf-16-le")                                                                 
        thread.exit_thread()
    
    def test(number):
        for n in range(number):
            thread.start_new_thread(start,())
            time.sleep(1)
    
    test(20) 
    

I can't understand why the file is garbled, I have use the fcntl flock to keep
the read/write sequence, where is the problem?


Answer: It's garbled because an fcntl lock is owned by a _process_ , not by a _thread_
, so a process cannot use fcntl to serialize its own access. See [this
answer](http://stackoverflow.com/a/9462648/132382), for example.

You'll need to use a threading construct like a
[Lock](http://docs.python.org/2/library/threading.html#lock-objects) instead.

