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

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
主站蜘蛛池模板: 山阴县| 柯坪县| 昭觉县| 泰和县| 苍溪县| 房产| 炎陵县| 竹溪县| 溆浦县| 元朗区| 伊金霍洛旗| 隆尧县| 长顺县| 邯郸县| 临沧市| 防城港市| 东兴市| 青龙| 永宁县| 鄂托克前旗| 佳木斯市| 达拉特旗| 安庆市| 荥经县| 正定县| 固始县| 武平县| 嘉黎县| 略阳县| 招远市| 黎城县| 邓州市| 涿鹿县| 曲阳县| 长子县| 竹山县| 永福县| 许昌市| 宁海县| 瓮安县| 垫江县|