Introduction to the read/write lock operation in python

Source: Internet
Author: User
This article mainly introduces the read/write lock operation method of the python version, and analyzes the principles and implementation skills of the Python read/write lock mechanism in the form of examples, you can refer to the example in this article to describe the read/write lock operation method of the python version. We will share this with you for your reference. The details are as follows:

The read/write lock mechanism is used recently, but the built-in library of python2.7 is actually available.
There are many examples of read/write locks on the Internet, but the principle is simple and the code is clear, but it is rare to write it by yourself.

Read/Write locks are generally used when one or more writers access a certain resource at the same time. Multiple readers can share resources, but resources are mutually exclusive between writers and readers.

This means that multiple readers or writers can be at work at the same time.

The read/write locks can be divided into three categories: reader first, writer first, and public policy.

First, the reader is preferred,
The purpose is to ensure that the reader can obtain resources without waiting as much as possible.
The main point of implementation is that writing is relatively simple, and only one lock is needed to control whether data can be written.
For readers, when the first reader arrives, it is necessary to stop the writer and then open the writer after the last reader leaves. There is a reader counter problem here. this counter needs to be shared among all readers, so an extra lock is required to ensure the atomicity of the addition and subtraction operations of this counter.

Implementation code:

Defines a class RWLock and performs simple initialization.

import threadingclass RWLock(object):  def __init__(self):    self.rlock = threading.Lock()    self.wlock = threading.Lock()    self.reader = 0

Writer lock

def write_acquire(self):  self.wlock.acquire()def write_release(self):  self.wlock.release()

Reader lock

def read_acquire(self):  self.rlock.acquire()  self.reader += 1  if self.reader == 1:    self.wlock.aquire()  self.rlock.release()def read_release(self):  self.rlock.acquire()  self.reader -= 1  if self.reader == 0:    self.wlock.release()  self.rlock.release()

For more information about the read/write lock operation methods in python, see the Chinese PHP website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.