简便快捷的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使用者提供的所有多线程机制接口。

  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. }; 

我们发现,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开发工具

2010-06-03 12:41:45

Linux 开发工具

2017-01-05 14:28:43

Linux内核代码源码

2011-08-05 10:13:45

iPhone开发工具 Cocoa Xcode

2020-11-19 10:05:03

Java

2010-02-24 14:53:33

Python开发工具

2010-01-21 10:44:59

Visual C++

2011-08-04 16:28:01

iPhone 开发工具 Accessoriz

2011-08-04 16:17:39

iPhone 开发工具

2010-06-10 17:45:03

Linux 开发工具

2010-01-08 17:01:46

C++开发工具

2010-03-24 13:52:53

Python GUI开

2010-05-28 13:38:29

Linux开发工具

2017-03-12 11:42:21

Linux致远电子内核源码

2012-10-12 09:20:19

AdobeAcrobat XI

2010-04-26 09:28:43

Oracle数据库

2010-03-01 15:57:59

Python开发工具

2022-03-10 08:44:50

Python开发工具

2022-10-27 15:57:26

开发工具鸿蒙
点赞
收藏

51CTO技术栈公众号