[Python] 多线程相关
3 月,跳不动了?>>> 1 相关基础 Python3 线程中常用的两个模块为: _thread threading(推荐使用) thread 模块已被废弃。用户可以使用 threading 模块代替。所以,在 Python3 中不能再使用"thread" 模块。为了兼容性,Python3 将 thread 重命名为 "_thread"。 1.1 _thread 模块函数式调用 _thread 提供了低级别的、原始的线程以及一个简单的锁,它相比于 threading 模块的功能还是比较有限的。 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程。语法如下: _thread.start_new_thread ( function, args[, kwargs] ) 参数说明: function - 线程函数。 args - 传递给线程函数的参数,他必须是个tuple类型。 kwargs - 可选参数。 1.2 threading 模块对象式调用 threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法。 除了使用函数外,线程模块同样可以通过直接从 threading.Thread 继承创建一个新的子类,并实例化后调用 start() 方法启动新线程。 import threading import time