多线程使得程序内部可以分出多个线程来做多件事情,充分利用CPU空闲时间,提升处理效率。python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补。并且在Python3中废弃了thread模块,保留了更强大的threading模块。
在python的原始解释器CPython中存在着GIL(Global Interpreter Lock,全局解释器锁),因此在解释执行python代码时,会产生互斥锁来限制线程对共享资源的访问,直到解释器遇到I/O操作或者操作次数达到一定数目时才会释放GIL。所以,虽然CPython的线程库直接封装了系统的原生线程,但CPython整体作为一个进程,同一时间只会有一个获得GIL的线程在跑,其他线程则处于等待状态。这就造成了即使在多核CPU中,多线程也只是做着分时切换而已。
如果你的程序是CPU密集型,多个线程的代码很有可能是线性执行的。所以这种情况下多线程是鸡肋,效率可能还不如单线程因为有上下文切换开销。但是如果你的代码是IO密集型,涉及到网络、磁盘IO的任务都是IO密集型任务,多线程可以明显提高效率,例如多线程爬虫,多线程文件处理等等
多线程爬虫的代码实例
注: 以下代码在python3下运行通过, python2版本差异较大,不能运行成功,如需帮助请下方留意。
# coding=utf-8import threading, queue, time, urllibfrom urllib import requestbaseUrl = 'http://www.pythontab.com/html/pythonjichu/'urlQueue = queue.Queue()for i in range(2, 10): url = baseUrl + str(i) + '.html' urlQueue.put(url) #print(url)def fetchUrl(urlQueue): while True: try: #不阻塞的读取队列数据 url = urlQueue.get_nowait() i = urlQueue.qsize() except Exception as e: break print ('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url)) try: response = urllib.request.urlopen(url) responseCode = response.getcode() except Exception as e: continue if responseCode == 200: #抓取内容的数据处理可以放到这里 #为了突出效果, 设置延时 time.sleep(1)if __name__ == '__main__': startTime = time.time() threads = [] # 可以调节线程数, 进而控制抓取速度 threadNum = 4 for i in range(0, threadNum): t = threading.Thread(target=fetchUrl, args=(urlQueue,)) threads.append(t) for t in threads: t.start() for t in threads: #多线程多join的情况下,依次执行各线程的join方法, 这样可以确保主线程最后退出, 且各个线程间没有阻塞 t.join() endTime = time.time() print ('Done, Time cost: %s ' % (endTime - startTime))
运行结果:
1个线程时:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/8.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Done, Time cost: 8.182249069213867
2个线程时:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/8.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Done, Time cost: 4.0987958908081055
3个线程时:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-3, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-4, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-4, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Current Thread Name Thread-3, Url: http://www.pythontab.com/html/pythonjichu/8.html Done, Time cost: 2.287320137023926
通过调节线程数可以看到,执行时间会随着线程数的增加而缩短,抓取效率成正比增加。
Python多线程在IO密集型任务,多线程可以明显提高效率,CPU密集型任务不适合使用多线程处理。