Python base-----Various locks in python

Source: Internet
Author: User

One, Global interpreter lock (GIL)

1, what is the global interpreter lock

In the same process, whenever one thread acquires the use rights of the Global Interpreter (CPU), the other threads must wait for the global Interpreter (CPU) of the thread to disappear before using the Global interpreter (CPU). Instant multiple threads do not affect each other directly under the same process and only one thread uses the CPU, such a mechanism is called the Global Interpreter Lock (GIL).

2. Benefits of Global interpreter lock

1, avoid a lot of locking unlock the benefits

2. Make data more secure and solve the data integrity and state synchronization between multiple threads

3. Disadvantages of global interpreter

Multi-core processors degenerate into single-core processors, and can only be parallelized concurrently.

4.:

    

A thread under a process at the same time can only be processed by one CPU, so the thread under the Gil lock can only be concurrency and cannot be parallelized.

5. Example:

    

Second, synchronous lock

1. What is a sync lock?

A thread in a process at the same time can only use one CPU, and to ensure that the program under this thread is being run by the CPU for a certain period of time, a synchronous lock will be used.

2, why use synchronous lock?

Because it is possible that when a thread is using the CPU, the program under that thread may experience IO operations, then the CPU will be cut to another thread, which may affect the integrity of the program's results.

3, how to use the sync lock?

Only the operation of locking and releasing locks is required before and after the operation of the public data.

4. Example:

    

· 5. Expand your knowledge

1. The role of the Gil: there must be competition for resources in multi-threaded situations, Gil is to ensure that threads at the interpreter level only use shared resources (CPUs).

2, the role of the synchronous Lock: In order to ensure that the interpreter level of their own program to write the only use of shared resources generated a synchronous lock.

Third, recursive lock and deadlock

1. What is a deadlock?

A phenomenon in which two or more threads or processes are waiting for resources in the process of executing a program.

    

2. What is a recursive lock?

In Python, in order to support multiple requests for the same resource in the same thread, Python provides a reentrant lock. The Rlock internally maintains a lock and a counter.

variables, counter records the number of acquire, so that resources can be require multiple times. Until all the acquire of a thread have been release, the other threads will be

Resources.

    

Iv. signal Volume (semaphore)

1, what is the signal volume?

As with the process, Semaphore manages a built-in counter whenever acquire () is called with a built-in function of 1, which is built into function +1 whenever the release () is called.

The counter cannot be 0 when the counter is 0 o'clock acquire () will block the thread until another thread calls release ().

2.:

    

    

    

    

Python base-----Various locks in python

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.