Python爬虫(十二)
学习Python爬虫过程中的心得体会以及知识点的整理,方便我自己查找,也希望可以和大家一起交流。
—— threading库应用详解 ——
threading库对应的是线程。
进程和线程的问题点击 详细教程查看。
threading多线程是python自带的,其创建多线程主要有2种方式。一种为继承threading类,另一种使用threading.Thread函数,接下来将会分别介绍这两种用法。
1. 继承threading类
此方法继承了threading类,并且重构了run函数功能。
最简单的继承threading类的线程:
注意:super(Counter, self).__init__(name = threadName)
这段代码是在定义线程类。
import threading, time, random
count = 0
class Counter(threading.Thread):
def __init__(self, lock, threadName):
'''@summary: 初始化对象。
@param lock: 锁对象。
@param threadName: 线程名称。
'''
super(Counter, self).__init__(name = threadName) #注意:一定要显式的调用父类的初始化函数。
self.lock = lock
def run(self):
#重写父类run方法,在线程启动后执行该方法内的代码。
global count
self.lock.acquire()
for i in range(100):
count = count + 1
self.lock.release()
lock = threading.Lock()
for i in range(5):
Counter(lock, "thread-" + str(i)).start()
time.sleep(2) #确保线程都执行完毕
print (count)
结果如图:
2. threading.Thread函数
最简单的threading.Thread函数的线程:
import threading, time, random
count = 0
lock = threading.Lock()
def doAdd():
'''@summary: 将全局变量count 逐一的增加10000。
'''
global count, lock
lock.acquire()
for i in range(10000):
count = count + 1
lock.release()
for i in range(5):
threading.Thread(target = doAdd, args=(), name = 'thread-' + str(i)).start()
time.sleep(2) #确保线程都执行完毕
print (count)
结果如图:
3. threading库的方法
-
Start() :开始线程的执行
-
Run() :定义线程的功能的函数
def run(self): global count self.lock.acquire() for i in range(100): count = count + 1 self.lock.release()
-
Join(timeout=None): 程序挂起,直到线程结束;如果给了timeout,则最多阻塞timeout秒
-
getName(): 返回线程的名字
-
setName() :设置线程的名字
-
isAlive(): 布尔标志,表示这个线程是否还在运行
-
isDaemon(): 返回线程的daemon标志
-
setDaemon(daemonic): 把线程的daemon标志设为daemonic(一定要在start()函数前调用)
-
t.setDaemon(True): 把父线程设置为守护线程,当父进程结束时,子进程也结束。
总的来说,threading库与multiprocessing库主要方法和函数相差不大,可以相互借鉴。