Introduction to simple and quick Python development tools

Source: Internet
Author: User

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.

 
 
  1. [thread1.py]  
  2.  
  3. import thread  
  4.  
  5. import time  
  6.  
  7. def threadProc():  
  8.  
  9.     print 'sub thread id : ', thread.get_ident()  
  10.  
  11.     while True:  
  12.  
  13.         print "Hello from sub thread ", thread.get_ident()  
  14.  
  15.         time.sleep(1)  
  16.  
  17. print 'main thread id : ', thread.get_ident()  
  18.  
  19. thread.start_new_thread(threadProc, ())  
  20.  
  21. while True:  
  22.  
  23.     print "Hello from main thread ", thread.get_ident()  
  24.  
  25.     time.sleep(1)  
  26. [threadmodule.c]  
  27.  
  28. static PyMethodDef thread_methods[] = {  
  29.  
  30.     {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,…},  
  31.  
  32.     {"start_new",    (PyCFunction)thread_PyThread_start_new_thread, …},  
  33.  
  34.     {"allocate_lock",    (PyCFunction)thread_PyThread_allocate_lock, …},  
  35.  
  36.     {"allocate",     (PyCFunction)thread_PyThread_allocate_lock, …},  
  37.  
  38.     {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, …},  
  39.  
  40.     {"exit",          (PyCFunction)thread_PyThread_exit_thread, …},  
  41.  
  42.     {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,…},  
  43.  
  44.     {"get_ident",       (PyCFunction)thread_get_ident, …},  
  45.  
  46.     {"stack_size",      (PyCFunction)thread_stack_size, …},  
  47.  
  48.     {NULL,          NULL}       /* sentinel */  
  49.  
  50. }; 

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.

  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.