- Mastering Concurrency in Python
- Quan Nguyen
- 326字
- 2021-06-10 19:24:02
The threading module in Python 3
The old thread module has been considered deprecated by Python developers for a long time, mainly because of its rather low-level functions and limited usage. The threading module, on the other hand, is built on top of the thread module, providing easier ways to work with threads through powerful, higher-level APIs. Python users have actually been encouraged to utilize the new threading module over the thread module in their programs.
Additionally, the thread module considers each thread a function; when the thread.start_new_thread() is called, it actually takes in a separate function as its main argument, in order to spawn a new thread. However, the threading module is designed to be user-friendly for those that come from the object-oriented software development paradigm, treating each thread that is created as an object.
In addition to all of the functionality for working with threads that the thread module provides, the threading module supports a number of extra methods, as follows:
- threading.activeCount(): This function returns the number of currently active thread objects in the program
- threading.currentThread(): This function returns the number of thread objects in the current thread control from the caller
- threading.enumerate(): This function returns a list of all of the currently active thread objects in the program
Following the object-oriented software development paradigm, the threading module also provides a Thread class that supports the object-oriented implementation of threads. The following methods are supported in this class:
- run(): This method is executed when a new thread is initialized and started
- start(): This method starts the initialized calling thread object by calling the run() method
- join(): This method waits for the calling thread object to terminate before continuing to execute the rest of the program
- isAlive(): This method returns a Boolean value, indicating whether the calling thread object is currently executing
- getName(): This method returns the name of the calling thread object
- setName(): This method sets the name of the calling thread object
- Implementing VMware Horizon 7(Second Edition)
- Linux C/C++服務器開發實踐
- 算法精粹:經典計算機科學問題的Java實現
- 算法精粹:經典計算機科學問題的Python實現
- Lua程序設計(第4版)
- C++面向對象程序設計習題解答與上機指導(第三版)
- Visual C#通用范例開發金典
- Android開發:從0到1 (清華開發者書庫)
- Advanced Express Web Application Development
- Python語言實用教程
- Nagios Core Administration Cookbook(Second Edition)
- Android應用開發實戰(第2版)
- UX Design for Mobile
- After Effects CC技術大全
- 數字媒體技術概論