SoFunction
Updated on 2024-11-07

Example of locking a Python multithreaded read/write file in detail

Python's multi-threaded in io than single-threaded still have an advantage, but in multi-threaded development, less read and write operations on the file. In the management of multiple threads on the same file read and write operations, it is necessary to file locks.

Using fcntl

Under linux, python's standard library has ready-made file locks from the fcntl module. This module provides interfaces to fcntl() and ioctl() for unix systems.

For file locking operations, you mainly need to use the (fd, operation) function.

The parameter fd represents the file descriptor; the parameter operation specifies the lock operation to be performed, and the parameter can take the following values:

LOCK_SH: Indicates that a shared lock is to be created, and that the shared lock for a file can be owned by more than one process at any given time

LOCK_EX: Indicates the creation of an exclusive lock, which can only be owned by one process at any given time for a file.

LOCK_UN: Indicates that the lock created by the process is deleted.

LOCK_MAND: It is mainly used for shared mode locking. It can be used in conjunction with LOCK_READ or LOCK_WRITE to indicate whether concurrent reads or concurrent writes are allowed.

demo

import fcntl
import threading
import time
 
 
def writetoTxt(txtFile):
 id = ().getName()
 with open(txtFile, 'a') as f:
  ((), fcntl.LOCK_EX) #Locked
  print "{0} acquire lock".format(id)
  ("write from {0} \r\n".format(id))
  (3)
 # Outside the WITH block, file closed, automatically unlocked #
 print "{0} exit".format(id)
 
 
for i in range(5):
 myThread = (target=writetoTxt, args=("",))
 ()

While the code is running, the console will print in turn which thread has acquired the lock and is reading or writing to the file.

Thread-1 acquire lock
Thread-1 exit
Thread-2 acquire lock
Thread-2 exit
Thread-3 acquire lock
Thread-3 exit
Thread-5 acquire lock
Thread-5 exit
Thread-4 acquire lock
Thread-4 exit

wrap-up

This is accomplished by calling the

((), fcntl.LOCK_EX)

Locks the file and blocks if any other thread tries to lock the test file.

The lock is automatically released when the thread finishes execution. Alternatively, you can take a proactive approach to unlocking the thread: call the

((),fcntl.LOCK_UN)

function to unlock the file test.

Using thread locks

When multiple threads share a single piece of data, synchronization must be controlled or there will be unanticipated results, i.e., "thread-unsafe"

Thread synchronization ensures that multiple threads have safe access to competing resources,The simplest synchronization mechanism is to introduce mutual exclusion locks。

Mutual exclusion locks introduce a state for resources:close with a latch/非close with a latch。

When a thread wants to change shared data,先将其close with a latch,The state of the resource at this point is“close with a latch”,Can't be changed in other threads;

Until the thread releases the resource,Change the state of the resource to“非close with a latch”,其他的线程才能再次close with a latch该资源。

Mutual exclusion locks ensure that only one thread performs a write operation at a time,This ensures the correctness of the data in a multi-threaded situation。

The Lock class is defined in the threading module to handle locking easily:

#Creating locks
mutex = ()
#Locked
([timeout])
#Unlocked
()

Demo

The code to implement the above example using a mutex lock is as follows:

import threading
import time
 
def writetoTxt(txtFile):
 id = ().getName()
 (10)
 with open(txtFile, 'a') as f:
  print "Thread {0} acquire lock".format(id)
  ("write from thread {0} \r\n".format(id))
  (3)
 ()
 print "Thread {0} exit".format(id)
 
 
mutex = ()
 
for i in range(5):
 myThread = (target=writetoTxt, args=("",))
 ()

(The above code is essentially a single thread of sequential execution)

Results:

Thread Thread-1 acquire lock
Thread Thread-1 exit
Thread Thread-2 acquire lock
Thread Thread-2 exit
Thread Thread-3 acquire lock
Thread Thread-3 exit
Thread Thread-4 acquire lock
Thread Thread-4 exit
Thread Thread-5 acquire lock
Thread Thread-5 exit

wrap-up

A lock enters the "locked" state when a thread calls the lock's acquire() method to obtain it. Only one thread can acquire the lock at a time. If another thread tries to acquire the lock, the thread will become "blocked", which is called "synchronous blocking".

The lock enters the "unlocked" state until the thread that owns it calls the release() method of the lock to release it. The thread scheduler selects one of the synchronously blocking threads to acquire the lock and puts that thread into the running state.

Above this on Python multi-threaded read and write file locking example details is all I have shared with you, I hope to give you a reference, but also hope that you support me more.