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

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
主站蜘蛛池模板: 海盐县| 新津县| 探索| 临猗县| 松原市| 山阴县| 繁峙县| 新宁县| 安泽县| 会同县| 葫芦岛市| 云林县| 昌邑市| 红原县| 即墨市| 岗巴县| 法库县| 苍山县| 平阴县| 西平县| 德昌县| 津南区| 林甸县| 桐城市| 龙南县| 慈溪市| 吉水县| 沙坪坝区| 开原市| 卢氏县| 涪陵区| 图木舒克市| 安化县| 万全县| 沧源| 涿州市| 潼关县| 荆门市| 迭部县| 招远市| 毕节市|