- 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
- Mastering Visual Studio 2017
- Web應用系統(tǒng)開發(fā)實踐(C#)
- Redis入門指南(第3版)
- LabVIEW程序設計基礎與應用
- 深入淺出Windows API程序設計:編程基礎篇
- Apache Karaf Cookbook
- 深入淺出Serverless:技術原理與應用實踐
- 學習OpenCV 4:基于Python的算法實戰(zhàn)
- D3.js By Example
- Android驅動開發(fā)權威指南
- 自學Python:編程基礎、科學計算及數(shù)據分析(第2版)
- INSTANT JQuery Flot Visual Data Analysis
- 深入理解Java虛擬機:JVM高級特性與最佳實踐
- Java Web動態(tài)網站開發(fā)(第2版·微課版)
- Mastering PyCharm