简便快捷的Python开发工具介绍

开发 后端
在Python开发工具里面最基础的多线程机制接口就是thread module,这个多线程的接口是用C语言来实现的,建立在thread module的基础上。

Python开发工具是一个具有更高层的多线程机制接口,比如threding module,threading module是一个标准库中的module,用Python语言实现,Python可以使用户避免过分的语法的羁绊而将精力主要集中到所要实现的程序任务上。

我们的目标是要剖析Python开发工具中的多线程机制是如何实现的,而非学习在Python中如何进行多线程编程,所以重点会放在thread module上。通过这个module,看一看Python对操作系统的原生线程机制所做的精巧的包装。

我们通过下面所示的thread1.py开始充满趣味的多线程之旅,在thread module中,Python向用户提供的多线程机制的接口其实可以说少得可怜。当然,也正因为如此,才使Python中的多线程编程变得非常的简单而方便。我们来看看在thread module的实现文件threadmodule.c中,thread module为Python使用者提供的所有多线程机制接口。

[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 */  
 
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

我们发现,thread module中有的接口居然以不同的形式出现了两次,比如“start_new_thread”“start_new”,实际上在Python开发工具内部,对应的都是thread_ PyThread_start_new_thread这个函数。所以,thread module所提供的接口,真的是少得可怜。在我们的thread1.py中我们使用了其中两个接口。关于这两个接口的详细介绍,请参阅Python文档。

【编辑推荐】

  1. 有关Python系统文件进行介绍指导
  2. 如何正确的使用Python函数
  3. 对Python 构建工具进行详细介绍分析
  4. PythonAndroid浅析Python优势所在
  5. 如何使用Python模块解析配置文件?
责任编辑:chenqingxiang 来源: CSDN
相关推荐

2010-03-15 15:55:00

Python开发工具

2010-03-10 09:55:56

Python开发工具

2017-01-05 14:28:43

Linux内核代码源码

2010-06-03 12:41:45

Linux 开发工具

2011-08-05 10:13:45

iPhone开发工具 Cocoa Xcode

2020-11-19 10:05:03

Java

2010-06-10 17:45:03

Linux 开发工具

2011-08-04 16:28:01

iPhone 开发工具 Accessoriz

2011-08-04 16:17:39

iPhone 开发工具

2010-01-21 10:44:59

Visual C++

2010-02-24 14:53:33

Python开发工具

2010-01-08 17:01:46

C++开发工具

2010-03-24 13:52:53

Python GUI开

2017-03-12 11:42:21

Linux致远电子内核源码

2012-10-12 09:20:19

AdobeAcrobat XI

2010-05-28 13:38:29

Linux开发工具

2010-04-26 09:28:43

Oracle数据库

2022-10-27 15:57:26

开发工具鸿蒙

2010-03-01 15:57:59

Python开发工具

2013-11-21 10:21:25

点赞
收藏

51CTO技术栈公众号