In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python main thread destroy sub-thread process is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
After the processing is completed, the Python main thread will destroy the thread. In fact, the destruction of the Python main thread is different from the destruction of the child thread, because the destruction of the main thread can only take effect by destroying the running environment of the Python, while the destruction of the child thread does not need to take these actions.
Python first cleans up the thread state object corresponding to the current thread through PyThreadState_Clear. The so-called cleaning is actually relatively simple, which is to maintain the reference count of the things maintained in the thread state object. The Python then releases the GIL, and the release of the GIL is done in the PyThreadState_DeleteCurrent.
In PyThreadState_DeleteCurrent, the current thread state object is first deleted, and then the GIL is released through PyEval_ReleaseLock. Python completes the destruction of most threads in the function PyThreadState_DeleteCurrent, and the rest of the PyThread_exit_thread is a platform-related operation, which completes different tasks of destroying native threads on each platform. Under Win32, you are actually calling _ endthread.
We know that the threads of Python are under the control of GIL, and the access to the entire Python interpreter and the C API provided by Python are mutually exclusive, which can be regarded as a mutual exclusion mechanism at the kernel level of Python. But this kind of mutual exclusion is beyond our control, and we need another controllable mutual exclusion mechanism-user-level mutual exclusion. Kernel-level mutex through GIL protects shared resources in the kernel. Similarly, user-level mutex protects shared resources in user programs. Consider the following example:
[thread2.py] import thread import time input = None lock = thread.allocate_lock () def threadProc (): while True: print 'sub thread id:' Thread.get_ident () print 'sub thread% d wait lock...'% thread.get_ident () lock.acquire () print' sub thread% d get lock...'% thread.get_ident () print 'sub thread% d receive input:% s'% (thread.get_ident () Input) print 'sub thread% d release lock...'% thread.get_ident () lock.release () time.sleep (1) thread.start_new_thread (threadProc, ()) print' main thread id:' Thread.get_ident () while True: print 'main thread% d wait lock...'% thread.get_ident () lock.acquire () print' main thread% d get lock...'% thread.get_ident () input = raw_input () print 'main thread% d release lock...'% thread.get_ident () lock.release () time.sleep (1)
In thread2.py, there is a variable input that is shared between the Python main thread and the child thread. This input is the user's input, the Python main thread receives the input, and the child thread prints the user input. In order to ensure that the child thread activates the print action after the user input, thread2.py uses the Lock mechanism provided by the Python thread mechanism to implement the synchronous action, which can actually be regarded as mutual exclusion between threads.
When the main thread obtains the lock through lock.acquire, it will have exclusive access to the input. The child thread suspends itself waiting for the lock until the main thread releases the lock and is not awakened by Python's thread scheduling mechanism to gain access to the input. Note that here the main thread needs to suspend itself using sleep in order to trigger the thread scheduling of Python and give the child thread a chance to run. At this time, because the main thread is waiting for lock, it will also suspend itself and can no longer access input.
Analysis of the problem of first contact with Python deployment
Full parsing of powerful and fast Python operation language
Introduction to the rich resources of Python debugger
On the Summary of Python Interactive skills
How to correctly understand the Python source file
Thus, from beginning to end, each thread can control its own use of input without having to worry about other threads breaking the state of input. This mechanism gives users the ability to control the interaction between threads, and is the core of thread mutual exclusion and synchronization in Python.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.