- threading.activeCo
python2.4前有个thread模块支持多线程编程,2.4后新增了threading模块,提供更高级别和更强大的线程支持.
- threading.activeCount():返回激活的线程对象的数量
- threading.currentThread():返回当前cpu执行的线程对象
- threading.enumerate(): 返回当前激活的线程对象列表
除此之外,threading提供了很多和java中Thread类一样的方法:(注意这是都是实例方法)
- run(): The run() method is the entry point for a thread.
- start(): The start() method starts a thread by calling the run method.
- join([time]): The join() waits for threads to terminate.
- isAlive(): The isAlive() method checks whether a thread is still executing.
- getName(): The getName() method returns the name of a thread.
- setName(): The setName() method sets the name of a thread.
创建线程的方式有两种:
1)继承threading.Thread
a.创建一个threading.Thread的子类
b.覆盖__init__(self [,args])
c.重写run方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
2)任务函数作为参数传递到Thread方法中
threading.Thread(target=task, args=()).start()
关注公众号「Python之禅」,回复「1024」免费获取Python资源