The Python development tool is a high-level multi-threaded interface. For example, the threding module and threading module are modules in a standard library, which are implemented in Python, python allows you to focus on the program tasks to be implemented by avoiding excessive syntax constraints.
Our goal is to analyze how the multi-thread mechanism in the Python development tool is implemented, rather than learning how to implement multi-threaded programming in Python, so we will focus on the thread module. Through this module, let's take a look at Python's exquisite packaging of the operating system's native thread mechanism.
We start a interesting multi-threaded journey through the thread1.py shown below. In the thread module, the multi-threaded interface provided by Python to users is actually very poor. Of course, this makes multi-threaded programming in Python very simple and convenient. Let's take a look at all the multi-threaded interfaces provided by thread module for Python users in threadmodule. c.
- [thread1.py]
-
- import thread
-
- import time
-
- def threadProc():
-
- print 'sub thread id : ', thread.get_ident()
-
- while True:
-
- print "Hello from sub thread ", thread.get_ident()
-
- time.sleep(1)
-
- print 'main thread id : ', thread.get_ident()
-
- thread.start_new_thread(threadProc, ())
-
- while True:
-
- print "Hello from main thread ", thread.get_ident()
-
- time.sleep(1)
- [threadmodule.c]
-
- static PyMethodDef thread_methods[] = {
-
- {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,…},
-
- {"start_new", (PyCFunction)thread_PyThread_start_new_thread, …},
-
- {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, …},
-
- {"allocate", (PyCFunction)thread_PyThread_allocate_lock, …},
-
- {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, …},
-
- {"exit", (PyCFunction)thread_PyThread_exit_thread, …},
-
- {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,…},
-
- {"get_ident", (PyCFunction)thread_get_ident, …},
-
- {"stack_size", (PyCFunction)thread_stack_size, …},
-
- {NULL, NULL} /* sentinel */
-
- };
We found that some interfaces in the thread module appear twice in different forms, such as "start_new_thread" and "start_new". In fact, they are inside the Python development tool, all corresponding functions are thread _ PyThread_start_new_thread. Therefore, the interfaces provided by the thread module are really poor. In thread1.py, we use two interfaces. For more information about these two interfaces, see the Python documentation.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?