Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python Thread Operation problems

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Python thread operation problem example analysis". In daily operation, I believe that many people have doubts in Python thread operation problem example analysis. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Python thread operation problem example analysis". Next, please follow the editor to study!

For example, consider the simplest case where each thread needs to access the thread_id information stored in the thread state object. Obviously, thread A should get the thread_id of A, and so should thread B. If thread A gets the thread_id of B, that would be bad. This means that there must be a mechanism within the Python thread, which is very similar to the mechanism that the operating system manages processes.

We know that when the operating system switches from process A to process B, the context of process An is first saved and then switched; when switching from process B to process A, the context of process An is restored, which ensures that process An is always running in its own context.

The thread state object here is equivalent to the context of the process, and Python also has a mechanism for storing and restoring thread state objects. At the same time, within Python, a global variable is maintained: PyThreadState * _ PyThread- State_Current. The thread state object corresponding to the currently active thread is stored in this variable when Python dispatches the thread. The thread state object corresponding to the activated thread is assigned to _ PyThreadState_Current so that it always holds the state object of the active thread.

This leads to the question: how does Python get the state object corresponding to the activated thread when scheduling the process? Python internally manages the state objects of all Python threads through an one-way linked list. When you need to find a state object corresponding to a thread, traverse the linked list and search for its corresponding state object. In the following description, we refer to this linked list as the "state object linked list".

Let's take a look at the key data structures that implement this mechanism. PyThread_create_key will create a new key. Notice that the key here is an integer. Also, when PyThread_create_key*** is called every time (the call in _ PyGILState_Init is *), a keymutex is created through PyThread_allcate_lock.

According to our previous analysis, this keymutex, like GIL, is actually a PNRMUTEX structure in which an Event kernel object under Win32 is maintained. The function of this keymutex is to mutually exclusive access to the linked list of state objects. In _ PyGILState_Init, the new key created is received by the global variable autoTLSkey maintained by Python, where TLS is the abbreviation for Thread Local Store.

This autoTLSkey will be used as a parameter for the Python thread to hold the state object of all threads, which is the key value in figure 15-6. That is, the key value in all key structures in the list of state objects will be autoTLSkey. Ah, the viewer said, you see, PyThread_create_key returns the incremental value of nkeys, that is to say, each time create, the result is different.

How can you say that all key are the same? In fact, in the entire Python source code, PyThread_create_key is only called in _ PyGILState_Init, and this _ PyGILState_Init is called only once when the Python runtime environment is initialized.

So how do you tell which thread corresponds to which state object? don't forget that we also have thread id. The id in figure 15-6 stores the id of each thread, and according to this id, you can obviously distinguish between different threads. Then the key in the figure looks a little superfluous. In fact, the linked list structure shown in figure 15-6 is not a pure linked list of state objects.

This key value makes sense when the value field of a key structure stores other objects related to the thread instead of the thread's state object. If we set one state object to S and the other to O, there are two key structures associated with a thread An in the linked list shown in figure 15-6.

Obviously, for these two key structures, the id domain is exactly the same, so when we need to pull the object O from the linked list instead of S, what should we use to distinguish O from S? It's this key value. So in fact, in Python, there may be many objects related to each thread, and each object will correspond to a key value that will be shared by all threads when storing such objects.

At this point, the study on "example analysis of Python thread operation problems" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report