Detailed explanation of Python thread application operations

Source: Internet
Author: User

The low-level thread control tools provided by Python thread applications include thread modules. Some developers who want to simplify multi-threaded applications, A advanced thread control library named threading is built based on the thread in Python.

In this section, we will analyze the specific implementation of threading. Before analyzing the specific implementation of threading, let's take a look at how threading is used. We know that through threading. thread creates multiple threads. There are two stages. The first stage is to call threading. thread. start, while the second stage is in threading. thread. threading. thread. run.

At the first stage, thread. start_new_thread has not been called to create a native subthread. At this time, the thread is recorded in _ limbo. Because no sub-thread is created, there is no thread id. The record method is _ limbo [thread] = thread.

In the second stage, the thread has been successfully called. when start_new_thread creates a native subthread, it will delete the subthread from _ limbo and record the subthread to _ active. The record method is _ active [thread_id] = thread. Visible.

The Python dict maintains the set of subthreads that have been created and are waiting to be created. The two dict Access _ active_limbo _ lock is protected. The threading module provides the operation to list all the current sub-threads: threading. enumerate. This operation is very simple, that is, to output the information of the thread set maintained in _ active and _ limbo.

In the thread module, Python provides a user-level thread synchronization tool: Lock Object. In the threading module, Python provides different tools for thread synchronization. To simplify the sequence of Python thread applications. The thread synchronization tools in threading are based on the Lock object provided by the thread.

By calling threading. Lock, we can create a Lock object in the thread. As described above, we can perform operations such as acquire and release on this object. Other thread synchronization tools in threading are based on this Lock object. Next we will give an overview of these thread synchronization tools. For specific implementation, see threading. py.

The RLock object is a variant of the Lock object. It maintains a Lock object internally, but it is a reentrant Lock. Generally, for Lock objects, if a thread performs the acquire operation twice in a row. Since there is no release after the first acquire, the second acquire will suspend the thread, which will directly cause the Lock Object To never release, so the thread is deadlocked.

The RLock object allows a thread to perform the acquire operation multiple times because it maintains the number of acquire threads through a counter variable. Each acquire operation must have a release operation. After all the release operations are completed, other threads can apply for the RLock object.

The Python thread application object is a packaging of the Lock Object. When creating a Condition object, its constructor needs a Lock object as a parameter. If this Lock object parameter is not available, condition will create an internal Rlock object.

You can also call acquire and release operations on the Condition object, because the internal Lock object itself supports these operations. However, the value of Condition lies in the wait and Policy semantics provided by Condition. Suppose there is A Condition Object C. When thread A calls C. wait (), thread A releases the Lock Object in thread C and enters the blocking state.

Wait until another thread calls C. Y (), A will apply for the Lock Object in C through acquire again, and exit the wait operation. The Semaphore object maintains a Condition object internally, which is useful for managing a group of shared resources. The Lock object can protect A shared resource, but if we have A shared resource pool, five of them share resource.

This means that five threads can freely access these resources at the same time. However, if you use Lock to protect shared resources, all threads will be mutually exclusive, this results in four resources A being wasted. Semaphore is a thread synchronization mechanism that protects shared resource pools based on Condition. Semaphore provides two operations: acquire and release, both of which have the same semantics as Lock.

When the thread calls Semaphore. when acquire is used, if there is still A remaining in the shared resource pool, the thread will continue to execute. If no resource exists in the resource pool, the thread will suspend itself, until other threads call Semaphore. release a resource.

Similar to Semaphore, Event objects are actually a kind of packaging for Condition objects, but provide unique set and wait semantics. The Event class code is very simple. If you are interested, refer to threading. py. In thread3.py, we can see that a key component in threading is threading. Thread. In this section, let's take a look at its implementation. In the implementation of threading. Thread, you will find many of the mechanisms we mentioned above.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.