官术网_书友最值得收藏!

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
主站蜘蛛池模板: 和硕县| 双辽市| 荆门市| 河西区| 华池县| 稻城县| 德化县| 日照市| 宝山区| 元谋县| 宜黄县| 微博| 拜城县| 庄浪县| 常熟市| 双江| 广昌县| 青神县| 沙雅县| 松溪县| 沂南县| 方正县| 孙吴县| 乐陵市| 沙洋县| 贵阳市| 沧州市| 宣武区| 永登县| 子长县| 桂平市| 张北县| 弋阳县| 铁力市| 永和县| 彰化县| 闽侯县| 顺平县| 久治县| 建湖县| 千阳县|